简体   繁体   English

语义 ui 复选框中的“全选/取消全选”

[英]'Select/Deselect All' in semantic ui checkboxes

I have seen a couple of other threads like this one, but I do not see why my code is not working.我已经看到了其他几个像这样的线程,但我不明白为什么我的代码不起作用。

I have a group of semantic UI checkboxes that should be checked when another checkbox called All is checked (similarly, all checkboxes should be unchecked when the All checkbox is unchecked).我有一组语义 UI 复选框,应在选中另一个名为All复选框时选中它们(同样,当取消选中All复选框时,应取消选中All复选框)。

 $('.custom').click(function() { $('input[name="other_checkboxes"]').prop('checked',this.checked); });
 <div> <div class="ui checkbox custom"> <input id="all_box" type="checkbox" /> <label>All</label> </div> <div class="ui form grouped fields ss-checkbox-input" id="other_checkboxes"> <div class="field"> <div class="ui checkbox"> <input type="checkbox" name="other_checkboxes" tabindex="0" value="A" /> <label>A</label> </div> </div> <div class="field"> <div class="ui checkbox "> <input type="checkbox" name="other_checkboxes" tabindex="0" value="B" /> <label>B</label> </div> </div> </div> </div>

A possible working solution.一个可能的工作解决方案。

 $("input[value='All']").change(function() { $("input[type='checkbox']").prop('checked', this.checked); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="ui form grouped fields ss-checkbox-input" id="myCheckboxes"> <div class="field"> <div class="ui checkbox"> <input type="checkbox" name="myCheckboxes" tabindex="0" value="All" /> <label>All</label> </div> </div> <div class="field"> <div class="ui checkbox"> <input type="checkbox" name="myCheckboxes" tabindex="0" value="A" /> <label>A</label> </div> </div> <div class="field"> <div class="ui checkbox "> <input type="checkbox" name="myCheckboxes" tabindex="0" value="B" /> <label>B</label> </div> </div> </div>

Note that this is div not input .请注意, thisdiv not input So instead of this.checked use $(e.target).prop("checked")所以代替this.checked使用$(e.target).prop("checked")

Try this one:试试这个:

 $('.custom').click(function (e) { $('input[name="other_checkboxes"]').prop('checked', $(e.target).prop("checked")); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div> <div class="ui checkbox custom"> <input id="all_box" type="checkbox" /> <label>All</label> </div> <div class="ui form grouped fields ss-checkbox-input" id="other_checkboxes"> <div class="field"> <div class="ui checkbox"> <input type="checkbox" name="other_checkboxes" tabindex="0" value="A" /> <label>A</label> </div> </div> <div class="field"> <div class="ui checkbox "> <input type="checkbox" name="other_checkboxes" tabindex="0" value="B" /> <label>B</label> </div> </div> </div> </div>

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

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