javascript - 根据其值选择单选按钮 jQuery/Javascript

标签 javascript jquery

我想要实现的目标可能很简单,但我卡住了。我很厌倦通过在页面加载时从对象中检索它的值来动态选择几个径向按钮。这是我正在尝试的:

var t_info = obj.tinfo;
var h_info = obj.hinfo;

$('input[value='+ t_info +']').attr('checked', 'checked');
$('input[value='+ t_info +']').change();
$('input[value='+ h_info +']').attr('checked', 'checked');
$('input[value='+ h_info +']').change();

页面加载时没有选择任何径向按钮。

最佳答案

如果您使用的是 jquery 1.6+,您应该使用 prop

Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value.

您还可以通过像这样的链接来删除 2 行代码。

$('input[value='+ t_info +']').prop('checked', true).change();
$('input[value='+ h_info +']').prop('checked', true).change();

还要确保将代码放入文档就绪函数中,以便它在尝试查找元素之前等待 dom 实际加载

$(document).ready(function(){

});

$(function(){

});

关于javascript - 根据其值选择单选按钮 jQuery/Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11460227/

相关文章:

javascript - Node q 循环遍历数组并追加数据

javascript - 没有方法 'has'

javascript - 我怎样才能停止 AJAX 调用保持 PHP session 事件

javascript - 如何对动态生成的按钮点击使用react?

javascript - jquery click 不起作用,尽管我已经包含在 document.ready 中

javascript - 仅当悬停在线条或图例上时才显示蜘蛛网的数据标签?

javascript - 带有 jQ​​uery 和 textarea 的动态 if/else 语句

javascript - 使用 JavaScript 解析 XML 文件中的链接

Javascript 增加输入结果并包含逗号

javascript - 为什么套接字连接在 onsubmit-event 内不起作用?