我有一个包含多个复选框组的表单。
每个复选框选项都有一个名为 itemtype
的自定义 html 5 属性。当 type="checkbox"的输入发生变化时,我需要评估刚刚选中的复选框。如果它的 data-itemtype
的值等于“Skipper”,我想取消选中属于同一组的所有其他复选框。
换句话说,假设我有多个复选框,并且其中一个复选框选项有一个名为“无”的标签,如果用户选中“无”,则不应选择“无”。我不能在这里使用单选按钮,因为我希望用户能够在未选择“无”的情况下选中多个选项。
这是我的代码分解
复选框组 1
<input name="control_307[0][307:1003]" id="item_307_1003_0" value="307:1003" data-itemtype="Answer" type="checkbox"> Zulauf Ltd<br>
<input name="control_307[0][307:361]" id="item_307_361_0" value="307:361" data-itemtype="Answer" type="checkbox"> Ziemann, McLaughlin and Kohler
<input name="control_307[0][308:1013]" id="item_307_1013_0" value="308:1013" data-itemtype="Skipper" type="checkbox"> None<br>
复选框组 2
<input name="control_1000[0][1000:999]" id="item_1000_999_0" value="307:1003" data-itemtype="Answer" type="checkbox"> First Options<br>
<input name="control_1000[0][1000:666]" id="item_1000_666_0" value="1000:666" data-itemtype="Answer" type="checkbox"> Some other option
<input name="control_1000[0][1000"123]" id="item_1000_123_0" value="308:1013" data-itemtype="Skipper" type="checkbox"> None<br>
我创建了一个 fiddle 来向您展示我对整个表格所做的工作 https://jsfiddle.net/8yf0v3xt/13/
我试过做这样的事情但是没有用
$(:checkbox).change(function(){
var skipper = $("input:checkbox[data-itemtype='Skipper']");
if( skipper.is(":checked")){
$(this).attr('checked', false); //uncheck all the boxes for the current group
skipper.attr('checked', true); //re-check the box that caused everything to uncheck
}
}).change();
如果选择“无”,我该怎么做才能取消所有选项?
最佳答案
希望这对你有用
$(:checkbox).change(function(){
var skipper = $("input:checkbox[data-itemtype='Skipper']");
if( skipper.is(":checked")){
//$(":checkbox").attr('checked', false); //uncheck all the boxes for the current group
//skipper.attr('checked', true); //re-check the box that caused everything to uncheck
$(":checkbox").not(skipper).prop("checked",false);//THIS IS IMPORTANT
}
}).change();
更新
更新 2
$(:checkbox).change(function(){
var skipper = $("input:checkbox[data-itemtype='Skipper']");
if( skipper.is(":checked")){
//$(":checkbox").attr('checked', false); //uncheck all the boxes for the current group
//skipper.attr('checked', true); //re-check the box that caused everything to uncheck
//$(":checkbox").not(skipper).prop("checked",false);//THIS IS IMPORTANT
//THIS IS IMPORTANT
$(this).closest(".survey-control-fieldset").find(":checkbox").not(skipper).prop("checked",false);
}
}).change();
更新 3
$(:checkbox).change(function(){
var skipper = $("input:checkbox[data-itemtype='Skipper']");
if( skipper.is(":checked")){
//$(":checkbox").attr('checked', false); //uncheck all the boxes for the current group
//skipper.attr('checked', true); //re-check the box that caused everything to uncheck
//$(":checkbox").not(skipper).prop("checked",false);//THIS IS IMPORTANT
//THIS IS IMPORTANT
$(this).closest(".survey-control-fieldset").find(":checkbox").not(skipper).prop("checked",false);
}
else
{
$(this).closest(".survey-control-fieldset").find(":checkbox[data-itemtype='Skipper']").prop("checked",false);
}
}).change();
结论
我注意到您的代码中有几点错误如下。
您正在使用
$(this).attr("checked",false);
取消选中所有 错误的复选框。$(this)
指向CURRENT SINGLE 仅 ELEMENT,并非全部。您使用的
.attr("checked",false)
也是不正确的,它 应该是.attr("checked","checked")
或.prop("checked",true)
.
关于javascript - 如何取消选中除使用 jQuery 的复选框以外的所有复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34521417/