Possible bug in jQuery 1.6 - $(…).attr(“checked”) is not working

被刻印的时光 ゝ 提交于 2019-12-10 17:48:19

问题


I have two radio buttons on my form and up until I started using jQuery 1.6 the following code worked fine:

<input type="radio" id="radio1" name="test"/>
<input type="radio" id="radio2" name="test"/>
<input type="button" onclick="testcheck()" value="Test"/>
<script>
function testcheck()
{
    if (jQuery("#radio1").attr("checked"))
        alert("first button checked");
    else if (jQuery("#radio2").attr("checked"))
        alert("second button checked");
    else
        alert("none checked")      
}
</script>

Once I start using jQuery 1.6, it always shows "none checked" because jQuery(radiobutton).attr("checked") is always empty.

Take a look at this jsfiddle, and change jQuery version between 1.5.2 and 1.6 to see what I mean.


回答1:


Take a look at this question: .prop() vs .attr()

Try this for your code instead:

function testcheck()
{
    if (jQuery("#radio1").prop("checked"))
        alert("first button checked");
    else if (jQuery("#radio2").prop("checked"))
        alert("second button checked");
    else
        alert("none checked")      
}

Also in the newest jQuery 1.6.1 they fixed some of the 1.6 attr problems




回答2:


I've been seeing this too. The other answers have some insights as to why this is, and when it'll be reverted (only for getters?); in the meantime, I've been using

$('#thingy').is(':checked');

as a cross-version workaround.

Hope this helps!




回答3:


This is not a bug but a change:

http://christierney.com/2011/05/06/understanding-jquery-1-6s-dom-attribute-and-properties/

Also, as mentioned by @Neal they have worked on this a bit in the latest 1.6.1 release candidate.

From the RC link:

Upgrading From 1.5.2 to 1.6.1 - With the introduction of the new .prop() method and the changes to the .attr() method, jQuery 1.6 sparked a discussion about the difference between attributes and properties and how they relate to each other. It also came with some backwards compatibility issues that have been fixed in 1.6.1. When updating from 1.5.2 to 1.6.1, you should not have to change any code.

There's a lot more explanation there but you might be able to skip to 1.6.1 and be fine...

EDIT - Added below on 5/16/11

John Resig just weighed in on the changes made around this and why.... Good read....

http://ejohn.org/blog/jquery-16-and-attr/




回答4:


I can't explain the change between versions, but there is a selector specifically looking for checked - http://api.jquery.com/checked-selector/




回答5:


You can hack it this way: jQuery("input[name='test']:checked")

The demo:

http://jsfiddle.net/8Eqpu/15/




回答6:


.attr() and .data() have changed dramatically in jQuery 1.6.

It's better explained on this article:

Upgrading to jQuery 1.6: Problems you may face

Hope this helps. Cheers



来源:https://stackoverflow.com/questions/5969598/possible-bug-in-jquery-1-6-attrchecked-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!