简体   繁体   English

取消选中所有项目的复选框

[英]uncheck the checkboxes for all item

I have the two checkboxes with checkAll and clearAll option.我有两个带有 checkAll 和 clearAll 选项的复选框。 When I click the geo clear all I want to clear the only geo checkboxes.当我单击地理清除所有我想清除唯一的地理复选框时。

 $(document).ready(function(){ $('span[data-action="clearAll"]').click(function(event) { $('span[data-action="clearAll"]').hide(); $('span[data-action="checkAll"]').show(); $(this).siblings("input:checkbox").each(function(){ $(this).prop("checked",false); $(this).parent('li').css('background-color','transparent'); }); }); $('span[data-action="checkAll"]').click(function(event) { $('span[data-action="checkAll"]').hide(); $('span[data-action="clearAll"]').show(); $(this).siblings("input:checkbox").each(function(){ $(this).prop("checked",true); }); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <div id="geo" class="checklistContainer" showselecteditems="true" style="width: 194.5px;"> <div class="findInList" id="geo_findInListDiv"><input type="text" value="Type here to search list..." id="geo_findInList" class="contains-placeholder blurred" style="width: 188.5px;"></div> <div class="actionButtons" id="geo_actionButtons"><span data-action="checkAll" style="">Check all</span> <span data-action="clearAll" style="display: none;">Uncheck all</span> </div> <div id="geo_checklist" class="checklist checklistHighlighted" style="position: relative; height: 70px; width: 196.5px;"> <ul class="checklist"> <li tabindex="0" class="even checked" style="background-color: transparent;"><input type="checkbox" value="US_East" name="geo[]" id="geo_US_East" checked="checked"><label for="geo_US_East" class="leaveRoomForCheckbox">US East</label></li> <li tabindex="0" class="even checked" style="background-color: transparent;"><input type="checkbox" value="NSU" name="geo[]" id="geo_NSU" checked="checked"><label for="geo_NSU" class="leaveRoomForCheckbox">NSU</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="US_West" name="geo[]" id="geo_US_West" checked="checked"><label for="geo_US_West" class="leaveRoomForCheckbox">US West</label></li> </ul> </div> </div> <div id="types" class="checklistContainer" showselecteditems="true" style="width: 194.5px;"> <div class="findInList" id="types_findInListDiv"><input type="text" value="Type here to search list..." id="types_findInList" class="blurred contains-placeholder" style="width: 188.5px;"></div> <div class="actionButtons" id="types_actionButtons"><span data-action="checkAll" style="">Check all</span> <span data-action="clearAll" style="display: none;">Uncheck all</span> </div> <div id="types_checklist" class="checklist" style="position: relative; height: 70px; width: 196.5px;"> <ul class="checklist"> <li tabindex="0" class="even" style="background-color: transparent;"><input type="checkbox" value="Uncategorized_lead" name="types[]" id="types_Uncategorized_lead" checked="checked"><label for="types_Uncategorized_lead" class="leaveRoomForCheckbox">test</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="Hold/Uncategorized" name="types[]" id="types_Hold_Uncategorized" checked="checked"><label for="types_Hold_Uncategorized" class="leaveRoomForCheckbox">test demo</label></li> <li tabindex="0" class="even" style="background-color: transparent;"><input type="checkbox" value="RGL" name="types[]" id="types_RGL" checked="checked"><label for="types_RGL" class="leaveRoomForCheckbox">test0</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="PGL" name="types[]" id="types_PGL" checked="checked"><label for="types_PGL" class="leaveRoomForCheckbox">test1</label></li> <li tabindex="0" class="even" style="background-color: transparent;"><input type="checkbox" value="MGL" name="types[]" id="types_MGL" checked="checked"><label for="types_MGL" class="leaveRoomForCheckbox">test2</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="1st_Presentation_/_Meeting" name="types[]" id="types_1st_Presentation_Meeting" checked="checked"><label for="types_1st_Presentation_Meeting" class="leaveRoomForCheckbox">test 3</label></li> </ul> </div> </div>

If I use $("input:checkbox").each() instead of $(this).siblings("input:checkbox") .It clear the all check box如果我使用$("input:checkbox").each()而不是$(this).siblings("input:checkbox")它清除所有复选框

I think you can do it like below snippet easily.我认为你可以像下面的代码片段那样轻松地做到这一点。 You don't need the each loop.您不需要each循环。

I have done an update after reviewing the question again.再次查看问题后,我进行了更新。 This resolves the issue as I see it.这解决了我看到的问题。 If anything please feel free to comment.如果有什么,请随时发表评论。

 $(document).ready(function(){ $('span[data-action="clearAll"]').click(function(event) { $(this).hide(); $(this).siblings('span[data-action="checkAll"]').show(); toggleCheckboxes($(this), false); }); $('span[data-action="checkAll"]').click(function(event) { $(this).hide(); $(this).siblings('span[data-action="clearAll"]').show(); toggleCheckboxes($(this), true); }); function toggleCheckboxes(node, check) { $(node).closest('.checklistContainer').find("ul.checklist input:checkbox").prop('checked', check); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="geo" class="checklistContainer" showselecteditems="true" style="width: 194.5px;"> <div class="findInList" id="geo_findInListDiv"><input type="text" value="Type here to search list..." id="geo_findInList" class="contains-placeholder blurred" style="width: 188.5px;"></div> <div class="actionButtons" id="geo_actionButtons"><span data-action="checkAll" style="">Check all</span> <span data-action="clearAll" style="display: none;">Uncheck all</span> </div> <div id="geo_checklist" class="checklist checklistHighlighted" style="position: relative; height: 70px; width: 196.5px;"> <ul class="checklist"> <li tabindex="0" class="even checked" style="background-color: transparent;"><input type="checkbox" value="US_East" name="geo[]" id="geo_US_East" checked="checked"><label for="geo_US_East" class="leaveRoomForCheckbox">US East</label></li> <li tabindex="0" class="even checked" style="background-color: transparent;"><input type="checkbox" value="NSU" name="geo[]" id="geo_NSU" checked="checked"><label for="geo_NSU" class="leaveRoomForCheckbox">NSU</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="US_West" name="geo[]" id="geo_US_West" checked="checked"><label for="geo_US_West" class="leaveRoomForCheckbox">US West</label></li> </ul> </div> </div> <div id="types" class="checklistContainer" showselecteditems="true" style="width: 194.5px;"> <div class="findInList" id="types_findInListDiv"><input type="text" value="Type here to search list..." id="types_findInList" class="blurred contains-placeholder" style="width: 188.5px;"></div> <div class="actionButtons" id="types_actionButtons"><span data-action="checkAll" style="">Check all</span> <span data-action="clearAll" style="display: none;">Uncheck all</span> </div> <div id="types_checklist" class="checklist" style="position: relative; height: 70px; width: 196.5px;"> <ul class="checklist"> <li tabindex="0" class="even" style="background-color: transparent;"><input type="checkbox" value="Uncategorized_lead" name="types[]" id="types_Uncategorized_lead" checked="checked"><label for="types_Uncategorized_lead" class="leaveRoomForCheckbox">test</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="Hold/Uncategorized" name="types[]" id="types_Hold_Uncategorized" checked="checked"><label for="types_Hold_Uncategorized" class="leaveRoomForCheckbox">test demo</label></li> <li tabindex="0" class="even" style="background-color: transparent;"><input type="checkbox" value="RGL" name="types[]" id="types_RGL" checked="checked"><label for="types_RGL" class="leaveRoomForCheckbox">test0</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="PGL" name="types[]" id="types_PGL" checked="checked"><label for="types_PGL" class="leaveRoomForCheckbox">test1</label></li> <li tabindex="0" class="even" style="background-color: transparent;"><input type="checkbox" value="MGL" name="types[]" id="types_MGL" checked="checked"><label for="types_MGL" class="leaveRoomForCheckbox">test2</label></li> <li tabindex="0" class="odd" style="background-color: transparent;"><input type="checkbox" value="1st_Presentation_/_Meeting" name="types[]" id="types_1st_Presentation_Meeting" checked="checked"><label for="types_1st_Presentation_Meeting" class="leaveRoomForCheckbox">test 3</label></li> </ul> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM