Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 为什么不选中复选框?_Jquery_Checkbox - Fatal编程技术网

Jquery 为什么不选中复选框?

Jquery 为什么不选中复选框?,jquery,checkbox,Jquery,Checkbox,代码: 为什么我要删除复选框test1 alert show checked 在JSFIDLE上测试您需要使用.prop而不是.attr 相关部分: <input type="checkbox" id="test1" class="check" checked=""> <input type="checkbox" id="test2" class="check" checked=""> <input type="checkbox" id="test3" class=

代码:

为什么我要删除复选框test1 alert show checked

在JSFIDLE上测试

您需要使用.prop而不是.attr

相关部分:

<input type="checkbox" id="test1" class="check" checked="">
<input type="checkbox" id="test2" class="check" checked="">
<input type="checkbox" id="test3" class="check" checked="">
<input type="checkbox" id="test4" class="check" checked="">

    <button class="start">Click</button>

$(".start").click(function(){

alert($("#test1").attr("checked"));

});
您需要使用.prop而不是.attr

相关部分:

<input type="checkbox" id="test1" class="check" checked="">
<input type="checkbox" id="test2" class="check" checked="">
<input type="checkbox" id="test3" class="check" checked="">
<input type="checkbox" id="test4" class="check" checked="">

    <button class="start">Click</button>

$(".start").click(function(){

alert($("#test1").attr("checked"));

});

我想这是因为您正在测试静态HTML

如果要测试选中的属性,请改用

elem.checked                     true (Boolean) Will change with checkbox state
$(elem).prop("checked")       true (Boolean) Will change with checkbox state
elem.getAttribute("checked")      "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6)     "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6.1+)  "checked" (String) Will change with checkbox state
$(elem).attr("checked") (pre-1.6) true (Boolean) Changed with checkbox state
这给了你正确或错误的答案


顺便说一句:属性应该是checked=checked或者根本不存在。

我想这是因为您正在测试静态HTML

如果要测试选中的属性,请改用

elem.checked                     true (Boolean) Will change with checkbox state
$(elem).prop("checked")       true (Boolean) Will change with checkbox state
elem.getAttribute("checked")      "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6)     "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6.1+)  "checked" (String) Will change with checkbox state
$(elem).attr("checked") (pre-1.6) true (Boolean) Changed with checkbox state
这给了你正确或错误的答案

顺便说一句:属性应该是checked=checked或根本不存在。

您混淆了.attr和.prop。您应该在这里使用。道具:

元素的属性是由标记设置的,您不能更改它。属性反映了它现在的状态。jQuery早期版本的实现中存在一些混乱,现在已经解决了。因此,一些在线资源仍然存在错误。

您混淆了.attr和.prop。您应该在这里使用。道具:


元素的属性是由标记设置的,您不能更改它。属性反映了它现在的状态。jQuery早期版本的实现中存在一些混乱,现在已经解决了。因此,某些联机资源仍然存在错误。

如果要在选中该复选框时运行警报,请使用以下命令:

alert($("#test1").prop("checked"));
范例


如果要在选中该复选框时运行警报,请使用以下命令:

alert($("#test1").prop("checked"));
范例

不确定,但尝试改用$test1:checked.length。不确定,但尝试改用$test1:checked.length。