jQuery 1.6 中可能存在的错误 - $(...).attr("checked") 不起作用

2024-04-17

我的表单上有两个单选按钮,直到我开始使用 jQuery 1.6 为止,以下代码工作正常:

<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>

一旦我开始使用 jQuery 1.6,它总是显示“未检查”,因为jQuery(radiobutton).attr("checked")总是空的。

看看这个jsfiddle http://jsfiddle.net/8Eqpu/7/,并在 1.5.2 和 1.6 之间更改 jQuery 版本,看看我的意思。


看看这个问题:.prop() 与 .attr() https://stackoverflow.com/questions/5874652/prop-vs-attr

请尝试使用您的代码:

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")      
}

也在最新的jQuery 1.6.1 http://blog.jquery.com/2011/05/10/jquery-1-6-1-rc-1-released/他们修复了 1.6 中的一些问题attr问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 1.6 中可能存在的错误 - $(...).attr("checked") 不起作用 的相关文章