Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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
Javascript 如何以编程方式取消选中复选框?_Javascript_Jquery - Fatal编程技术网

Javascript 如何以编程方式取消选中复选框?

Javascript 如何以编程方式取消选中复选框?,javascript,jquery,Javascript,Jquery,假设我有以下样本: <input name="Opt1" id="Opt1" type="checkbox" value="1" alt="1948" title="RQlevel1" src="2" checked="checked" onclick="levels();"/> <label style="cursor:pointer" for="OptID12851948">PADI Advanced Open Water</label> <in

假设我有以下样本:

<input name="Opt1" id="Opt1" type="checkbox" value="1" alt="1948" title="RQlevel1" src="2" checked="checked" onclick="levels();"/>  <label style="cursor:pointer" for="OptID12851948">PADI Advanced Open Water</label>

<input name="Opt2" id="Opt2" type="checkbox" value="2" alt="1953" title="RQlevel2" src=""  onclick="levels();"/>  <label style="cursor:pointer" for="OptID19521953">PADI Rescue</label>

<input name="Opt3" id="Opt3" type="checkbox" value="3" alt="1957" title="RQlevel2" src=""  onclick="levels();"/>  <label style="cursor:pointer" for="OptID19521953">PADI Rescue2</label>
使用:

或者对于jQuery的最新版本,您可以使用:


如果您使用jQuery1.6及更高版本,您应该真正使用它

此外,选择器已经确定输入已被选中。不需要通过检查属性或属性来复制它

if ($("input[value='1']:checked").length) { 
    $("input[value='1']:checked").prop("checked", false); 
}    
但是,如果您使用的jQuery版本低于1.6,则可以使用removeAttr()

$("input[value='1']:checked").removeAttr('checked');
$("input[value='1']:checked").prop('checked', false);
if ($("input[value='1']:checked").length) { 
    $("input[value='1']:checked").prop("checked", false); 
}    
if ($("input[value='1']:checked").length) { 
    $("input[value='1']:checked").removeAttr("checked"); 
}