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