2016-01-21 80 views
0

我手动选中了使用.attr('checked', true);的单选按钮(因为prop('checked', true);在我的情况下似乎没有做任何事情)。尽管将“checked”属性设置为“checked”,jQuery选中的单选按钮仍未被检查

虽然单选按钮确实得到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> 
+0

请张贴一个完整的代码示例。 – j08691

+0

你使用任何插件的单选按钮像CSS样式? –

+0

是的,但是CSS可以防止单选按钮被查看,即使它确实被检查过吗? – drake035

回答

2

始终使用prop打底层元素的属性,而不是attr

$('.variations_fieldset input[value="Every 2 months"]').prop('checked', true); 

attr只是改变DOM但确实并不总是设置基础属性。

请注意:prop不会更新attr,因此您无法在DOM检查器中看到更改。

的jsfiddle:https://jsfiddle.net/e0tpgekm/1/

+1

OP说已经试过了。 – shawnt00

+1

属性更改不会将“已选中”属性添加到单选按钮。 – Rycochet

+0

@ shawnt00:OP可能还没有意识到,如果你使用prop,'attr'的改变在DOM中不可见。你不能总是在表面上看到这样的表述。 –

-2

如果您使用jQuery 1.6之前的尝试:

.attr('checked','checked'); 
相关问题