admin管理员组文章数量:1194938
I've been trying to programatically select radio buttons with jQuery, something I thought would be as simple as changing the checked attribute.
However, the following code doesn't seem to do what is expected in jQuery 1.9.1 in Chrome/Firefox.
Expected behaviour: Click the div enclosing the radio button -> 'checked' attribute gets set -> renders checked in the DOM.
Actual behaviour: Click the div enclosing the radio button -> 'checked' attribute gets set -> renders checked in the DOM for the first and second button clicked, subsequent buttons don't render as checked.
jQuery:
$('div.form-type-radio').on('click', function () {
var Id = $(this).find('input[type=radio]').attr('id');
$('form input[type=radio]:not(#'+Id+')').removeAttr('checked');
$('#' + Id).attr('checked', 'checked');
console.log($('#' + Id));
});
Here's a jsFiddle - /
I've tried the same code with previous versions of jQuery and it all works as expected.
I've been trying to programatically select radio buttons with jQuery, something I thought would be as simple as changing the checked attribute.
However, the following code doesn't seem to do what is expected in jQuery 1.9.1 in Chrome/Firefox.
Expected behaviour: Click the div enclosing the radio button -> 'checked' attribute gets set -> renders checked in the DOM.
Actual behaviour: Click the div enclosing the radio button -> 'checked' attribute gets set -> renders checked in the DOM for the first and second button clicked, subsequent buttons don't render as checked.
jQuery:
$('div.form-type-radio').on('click', function () {
var Id = $(this).find('input[type=radio]').attr('id');
$('form input[type=radio]:not(#'+Id+')').removeAttr('checked');
$('#' + Id).attr('checked', 'checked');
console.log($('#' + Id));
});
Here's a jsFiddle - http://jsfiddle.net/GL9gC/
I've tried the same code with previous versions of jQuery and it all works as expected.
Share Improve this question edited Apr 6, 2013 at 9:21 dsgriffin 68.6k17 gold badges140 silver badges138 bronze badges asked Apr 5, 2013 at 22:46 SimonSimon 1,8693 gold badges23 silver badges30 bronze badges 2 |2 Answers
Reset to default 24In this case, you should use prop() instead of attr()/removeAttr().
Here's a working jsFiddle.
jQuery:
$('div.form-type-radio').on('click', function () {
var Id = $(this).find('input[type=radio]').prop('id');
$('form input[type=radio]:not(#'+Id+')').prop('checked');
$('#' + Id).prop('checked', 'checked');
console.log($('#' + Id));
});
LIVE DEMO
$('div.form-type-radio').on('click', function () {
$(this).find(':radio').prop({checked: true});
});
http://api.jquery.com/prop/
本文标签: javascriptIs there a bug with radio buttons in jQuery 191Stack Overflow
版权声明:本文标题:javascript - Is there a bug with radio buttons in jQuery 1.9.1? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738452588a2087571.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
not
works with elements as well). Your entire function could be just$('input[type=radio]', this).prop('checked', true);
– adeneo Commented Apr 5, 2013 at 22:51