【发布时间】:2016-01-21 23:25:50
【问题描述】:
我使用.attr('checked', true); 手动检查了一个单选按钮(因为prop( 'checked', true); 在我的情况下似乎没有做任何事情)。
虽然单选按钮在标记中确实获得了等于checked 的checked 属性,但单选按钮仍未选中(在页面上,所有单选按钮仍处于未选中状态) .
这怎么可能?
jQuery:
$( '.variations_fieldset input[value="Every 2 months"]' ).attr('checked', true);
获得的HTML:
<fieldset class="variations_fieldset">
<input type="radio" data-attribute_name="attribute_billing" name="attribute_billing" id="billing1" value="Once Off">
<label for="billing1">Once Off</label>
<input type="radio" data-attribute_name="attribute_billing" name="attribute_billing" id="billing2" value="Every 1 month">
<label for="billing2">Every 1 month</label>
<input type="radio" data-attribute_name="attribute_billing" name="attribute_billing" id="billing3" value="Every 2 months" checked="checked">
<label for="billing3">Every 2 months</label>
</fieldset>
【问题讨论】:
-
请发一个完整的代码示例。
-
你是否在使用任何单选按钮插件,例如 css 样式?
-
是的,但是 CSS 是否可以防止单选按钮看起来被选中,即使它确实被选中了?
-
在您的示例中,您在最后一个输入中已经有
checked="checked",但即使没有它,jQuery 也可以工作:jsfiddle.net/j08691/e0tpgekm -
属性更新实际上并没有添加您可以看到的属性-它会更改内部状态,因此
.prop("checked", true)是正确的方法-您可以使用$(":checked")查找结果
标签: jquery radio-button