[英]How to uncheck all checkboxes, if one checkbox checked?
I have a set of checkboxes, something like this: 我有一组复选框,如下所示:
<label class="checkbox-inline">
<input type="checkbox" id="" value="1"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="2"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="3"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="4"> any
</label>
Using these checkboxes, users can make make selection. 使用这些复选框,用户可以进行选择。 But if a user selects the "any" checkbox then I need to deselect all other selection. 但是如果用户选择“任何”复选框,那么我需要取消选择所有其他选择。
I tried it something like this, but it doesn't work for me. 我试过这样的东西,但它对我不起作用。
$('.checkbox-inline').click(function(){
$("input[type='checkbox']").attr('checked', false);
$(this).attr('checked', true);
})
Can anybody tell me how can I do this in jquery? 任何人都可以告诉我如何在jquery中执行此操作?
NOTE: I am using dynamically generated HTML for my checkboxes. 注意:我使用动态生成的HTML作为我的复选框。
Define an id
into the any checkbox
, like this: 在任意checkbox
定义一个id
,如下所示:
<label class="checkbox-inline">
<input type="checkbox" id="" value="1"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="2"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="3"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="any-checkbox" value="4"> any
</label>
I suggest this because it is not a good idea to use the fact that it is the fourth. 我建议这样做是因为使用它是第四个这个事实并不是一个好主意。 If you add another checkbox in the future before it, then it will no longer be the fourth. 如果您在将来添加另一个复选框,那么它将不再是第四个。
//We use .on to tackle with the dynamic nature of the HTML
$( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
if ($(this).attr("id") === "any-checkbox") { //Any checkbox tick
if ($(this).prop("checked")) { //User checked any
//Other checkboxes are unchecked
$(".checkbox-inline > input[type=checkbox]:not(#any-checkbox)").prop("checked", false)
}
} else { //Other checkbox tick
if ($(this).prop("checked")) {//User checked another checkbox
//Any checkbox is unchecked
$("#any-checkbox").prop("checked", false);
}
}
});
EDIT: As per the comment, I have solved the issue for multiple groups, like this: 编辑:根据评论,我已解决了多个组的问题,如下所示:
<label class="checkbox-inline">
<input type="checkbox" id="" name="hairColor" value="1"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="hairColor" value="2"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="hairColor" value="3"> information
</label>
<label class="checkbox-inline">
<input type="checkbox" id="hairColor" name="hairColor" value="4" class="any"> any
</label>
<br>
<label class="checkbox-inline">
<input type="checkbox" id="" name="eyeColor" value="1"> eyeColor
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="eyeColor" value="2"> eyeColor
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" name="eyeColor" value="3"> eyeColor
</label>
<label class="checkbox-inline">
<input type="checkbox" id="eyeColor" name="eyeColor" class="any" value="4"> any
</label>
//We use .on to tackle with the dynamic nature of the HTML
$( "body" ).on( "change", ".checkbox-inline > input[type=checkbox]", function() {
if ($(this).hasClass("any")) { //Any checkbox tick
if ($(this).prop("checked")) { //User checked any
//Other checkboxes are unchecked
$(".checkbox-inline > input[type=checkbox][name=" + $(this).attr("name") + "]:not(.any)").prop("checked", false)
}
} else { //Other checkbox tick
if ($(this).prop("checked")) {//User checked another checkbox
//Any checkbox is unchecked
$("[name=" + $(this).attr("name") + "].any").prop("checked", false);
}
}
});
Try this, it is fulfill your requirements 试试这个,它符合您的要求
$(document).on("change", "input", function() { if ($("input[value=4]").is(":checked")) { $("input[value!=4]").attr("checked", false); } }); $(document).ready(function() { for (i = 1; i < 4; i++) { $("#container").append("<label class='checkbox-inline'> <input type='checkbox' id='' value='" + i + "'>information</label>") } $("#container").append("<label class='checkbox-inline'> <input type='checkbox' id='' value='4'>any</label>"); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="container"></div>
Hi I edited my answer, 嗨,我编辑了我的答案,
The funny part is, most of us is focusing on the click of the label but what were trying to capture is the click event of the checkbox @.@ 有趣的是,我们大多数人都专注于标签的点击,但是试图捕获的是@ @复选框的点击事件。
I made this work by modfying ur code : 我通过修改你的代码来完成这项工作:
<label class="checkbox-inline">
<input class ="checkbox1" type="checkbox" id="" value = "1" > information
</label>
<label class="checkbox-inline">
<input class ="checkbox1" type="checkbox" id="" value = "2"> information
</label>
<label class="checkbox-inline">
<input class ="checkbox1" type="checkbox" id="" value = "3"> information
</label>
<label class="checkbox-inline">
<input class ="checkbox1" type="checkbox" id="" value = "4"> any
</label>
$('.checkbox1').click(function(){
$("input[type='checkbox']").removeAttr('checked');
$(this).prop('checked', true);
})
I added separate class for the checkboxes and trap its click event. 我为复选框添加了单独的类并捕获其click事件。 It works!! 有用!! :P :P
EDIT : If "any" checkbox has a special logic, 编辑:如果“任何”复选框有一个特殊的逻辑,
$('.checkbox1').click(function(){
if ($(this).val() != 4 && $("input[value=4]").is(":checked"))
return false;
// Check if any was checked
if ($(this).val() == 4)
$("input[value!=4]").removeAttr('checked');
//$(this).prop('checked', true);
})
Two scenarios trapped. 陷入困境的两种情况。 1. Unchecked other checkboxes if any checkbox was checked 2. Do not allow checking of other checkboxes if any was checked 1.如果选中任何复选框,则取消选中其他复选框2.如果选中任何复选框,则不允许检查其他复选框
PS I reused some code from answer of @Alex :D PS我从@Alex:D的回答中重用了一些代码
You can use this jquery code where input_id will be id of your checkboxes. 您可以使用此jquery代码,其中input_id将是您的复选框的id。
$(document).on('click', "#input_id :checkbox", function() {
if ($(this).is(':checked')) {
$("#input_id :checkbox").prop('checked', false);
$(this).prop('checked', true);
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.