简体   繁体   English

选中所有复选框javascript

[英]select all check boxes javascript

Hi I'm trying to "check all or deselect all" check boxes in my rails app. 嗨,我正在尝试在我的Rails应用程序中“全部选中或取消全部选中”复选框。 it does working when I use the checkbox and JS. 当我使用复选框和JS时,它确实起作用。 But, I also want the user the have the same function with links as well. 但是,我也希望用户也具有与链接相同的功能。 So, when the click on the link it will "check all or deselect all" checkboxes. 因此,当单击链接时,它将“全部选中或取消全部选中”复选框。

Thanks, 谢谢,

 <!-- Split button -->
    <div class="btn-group">
     <button type="button" class="btn btn-warning"><input type="checkbox" id="selecctall" value="selectAll"></button>
    <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
      <span class="caret"></span>
    </button>

    <ul class="dropdown-menu" role="menu">
      <li><a id="selectAll"  title="Click to do something" href="#" onclick="check();return false;">Select All</a></li>
      <li><a id="selectAll" title="Click to do something" href="#" onclick="uncheck();return false;">None</a></li>
    </ul>
  </div>

This is the loop where next to each user I have the checkbox. 这是每个用户旁边都有复选框的循环。

<% @users.each do |user| %>
   <tr class="<%= cycle("even", "odd") %>">
    <td><input id= "user_id_<%="#{user.id}"%>" name="user_ids[]" value=<%= user.id %> type="checkbox" class="checkbox1"></td>
    <td><%= user.full_name %></td>
    <td><%= user.email %></td>
    <td>
  <% end %>

This is the js at the bottom of the page 这是页面底部的js

<% content_for :javascripts do %>
      <script >
        function check() {
          if(document.getElementById("selecctall").checked = true;
        }

       function uncheck() {
         document.getElementById("selecctall").checked = false;
      }

     $(document).ready(function() {
        $('#selecctall').click(function(event) {
            if(this.checked) {
                $('.checkbox1').each(function() {
                    this.checked = true;
                });
            }else{
                $('.checkbox1').each(function() {
                    this.checked = false;
                });
            }
        });

     });
     $('body').on('hidden.bs.modal', '.modal', function () {
      $(this).removeData('bs.modal');
     });
   </script>

Note that the ID of an element must be unique . 请注意, 元素的ID必须是唯一的

When looking up an element by ID, JavaScript will match only a single element, as it assumes that the contract that IDs are unique is respected. 当按ID查找元素时,JavaScript将仅匹配单个元素,因为它假定ID唯一的约定得到遵守。

If you have two links, one for " select all " and one for " select none ", give each of them different IDs (or class names), and have a different $(...).click(function() { ... }); 如果您有两个链接,一个用于“全 ”,一个用于“全 ”,为每个链接赋予不同的ID(或类名),并具有不同的$(...).click(function() { ... }); callback for each of them. 回调每个。

For one, it simply does the check , and for another, it does the uncheck . 对于一个,它只执行check ,而对于另一个,它执行uncheck

Notice how your click callback has an if (this.checked) ... part. 请注意,您的click回调如何包含if (this.checked) ...部分。 This works if this is a checkbox, which it no longer is. 如果this是一个不再适用的复选框,则this方法有效。 Depending on which link is pressed, you already know if you intend to check or uncheck, which is why separate callbacks for each link makes sense. 根据按下哪个链接,您已经知道要检查还是取消选中,这就是每个链接单独回调有意义的原因。

As EyasSH stated you should never use duplicate IDs as it is a terrible practice and invalid. 正如EyasSH所述,您永远不应使用重复的ID,因为这是一种糟糕的做法,而且是无效的。 This is why $('#selectall') returns only one single element. 这就是$('#selectall')仅返回一个元素的原因。 BUT you can change the selector to make it find all instances of the duplicate IDs by changing it to $('[id="selectall"]') 但是您可以通过将选择器更改为$('[id="selectall"]')来更改选择器,以使其找到重复ID的所有实例。

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

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