为什么.attr("checked",true)在Jquery 1.7及更高版本中不起作用

Yon*_*eek 2 jquery

这段代码适用于jQuery,ver.1.6

<input type="checkbox" class="radio" value="1" name="sameName" />
<input type="checkbox" class="radio" value="1" name="sameName" />
<input type="checkbox" class="radio" value="1" name="sameName" />

$("input:checkbox").click(function() {
    if ($(this).attr("checked") === true) {
        var group = "input:checkbox[name='" + $(this).attr("name") + "']";
        $(group).attr("checked", false);
        $(this).attr("checked", true);
    } else {
        $(this).attr("checked", false);
    }
});?
Run Code Online (Sandbox Code Playgroud)

但是如果我使用jQuery 1.7的新方法更改代码它不会工作?为什么?谢谢你的时间:

$("input:checkbox").on('click', function() {
    if ($(this).attr("checked") === true) {
        var group = "input:checkbox[name='" + $(this).attr("name") + "']";
        $(group).attr("checked", false);
        $(this).attr("checked", true);
    } else {
        $(this).attr("checked", false);
    }
});?
Run Code Online (Sandbox Code Playgroud)

Bla*_*ger 7

.prop('checked',true)而不是.attr.

http://api.jquery.com/prop

此外,if ($(this).attr("checked") === true)可以简化为简化

if ($(this).prop("checked")) {
Run Code Online (Sandbox Code Playgroud)