javascript - 如何创建一个函数来选择一组复选框的子集?

标签 javascript jquery checkbox

我有以下一组复选框:

<ul class="checkbox_list">
 <li><input name="mensajes[receptores_list][]" type="checkbox" value="5" id="mensajes_receptores_list_5" /><label for="mensajes_receptores_list_5">Carlos (Admin)</label></li>
<li><input name="mensajes[receptores_list][]" type="checkbox" value="1" id="mensajes_receptores_list_1" /><label for="mensajes_receptores_list_1">Jorge (Admin)</label></li>
<li><input name="mensajes[receptores_list][]" type="checkbox" value="3" id="mensajes_receptores_list_3" /><label for="mensajes_receptores_list_3">Marisa (Admin)</label></li>
<li><input name="mensajes[receptores_list][]" type="checkbox" value="6" id="mensajes_receptores_list_6" /><label for="mensajes_receptores_list_6">Christian (Apuntes)</label></li>
<li><input name="mensajes[receptores_list][]" type="checkbox" value="4" id="mensajes_receptores_list_4" /><label for="mensajes_receptores_list_4">Julieta (Contable)</label></li>
<li><input name="mensajes[receptores_list][]" type="checkbox" value="2" id="mensajes_receptores_list_2" /><label for="mensajes_receptores_list_2">Vicky (Contable)</label></li>
</ul> 

使用 javascript 或 jquery,如何创建一个函数来选中/取消选中一组复选框的子集?

例如:我希望当我按下标有“仅选择(管理员)”的按钮时,仅选中/取消选中“Carlos(管理员)”和“Marisa(管理员)”和“Jorge(管理员)”和当我按下另一个标有“仅选择(Contable)”的按钮时,仅选中/取消选中:“Julieta(Contable)”和“Vicky(Contable)”。最后,第三个按钮标记为“全选”,以启用选中/取消选中的所有复选框。

我有一个新问题。我使用这组复选框作为表单的一部分。当我单击表单中的“提交”按钮时,所有复选框都被激活。 如何将四个按钮“Admin”、“Contable”、“Apuntes”和“Select All”替换为四个与按钮具有相同名称和相同功能的复选框?

最佳答案

实现此目的的一个好方法是将数据属性分配给输入,以便您可以正确过滤它们。

这是一个 JS fiddle :http://jsfiddle.net/46ABR/

HTML

<ul id="listOfOptions">
 <li>
    <input name="mensajes[receptores_list][]" type="checkbox" value="5" id="mensajes_receptores_list_5" data-role="admin"/>
    <label for="mensajes_receptores_list_5">Carlos (Admin)</label>
  </li>
  <li>
    <input name="mensajes[receptores_list][]" type="checkbox" value="1" id="mensajes_receptores_list_1" data-role="admin" />
    <label for="mensajes_receptores_list_1">Jorge (Admin)</label>
  </li>
  <li>
    <input name="mensajes[receptores_list][]" type="checkbox" value="3" id="mensajes_receptores_list_3" data-role="admin" />
    <label for="mensajes_receptores_list_3">Marisa (Admin)</label>
  </li>
  <li>
    <input name="mensajes[receptores_list][]" type="checkbox" value="6" id="mensajes_receptores_list_6" data-role="apuntes" />
    <label for="mensajes_receptores_list_6">Christian (Apuntes)</label>
  </li>
  <li>
    <input name="mensajes[receptores_list][]" type="checkbox" value="4" id="mensajes_receptores_list_4" data-role="contable" />
    <label for="mensajes_receptores_list_4">Julieta (Contable)</label>
  </li>
<li>
    <input name="mensajes[receptores_list][]" type="checkbox" value="2" id="mensajes_receptores_list_2" data-role="contable" />
    <label for="mensajes_receptores_list_2">Vicky (Contable)</label>
  </li>
</ul>

<button class="optionsFilterButton" data-role="admin">Select all Admin</button>
<button class="optionsFilterButton" data-role="apuntes">Select all Apuntes</button>
<button class="optionsFilterButton" data-role="contable">Select all Contable</button>
<button class="optionsFilterButton" data-role="">Select all</button>

Javascript

$('.optionsFilterButton').on("click", function(e) {
    var currentButtonEl = $(e.currentTarget);

    $('#listOfOptions input[type=checkbox]').prop('checked', false);

    var role = currentButtonEl.data('role');    
    if (role) {
      $('#listOfOptions input[type=checkbox][data-role=' + role + ']').prop('checked', true);
    } else {
      $('#listOfOptions input[type=checkbox]').prop('checked', true);
    }   
});

关于javascript - 如何创建一个函数来选择一组复选框的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19436846/

相关文章:

javascript - 悬停并单击图像以显示内容(显示初始内容)

javascript - 在 Javascript 文本框中名称不喜欢

javascript - 有没有办法将输入标签的 id 作为 onclick 函数参数传递,而无需在 JSX 中实际写入 id 值?

javascript - 根据父链接更改图像源?

jquery - 如何通过ajax将集合/数组发送到mvc操作

javascript - 包含文件上的 jquery .html

javascript - 如何在 HTML 中实现 "select all"复选框?

css - 如何在一行中显示 2 个复选框?

javascript - Chrome 扩展程序 - 背景页面和未读项目

Angular - 如何使用响应式(Reactive)表单动态默认选中复选框