页面中的全选/取消全选复选框

9
我看过这个,但对我的代码没有影响。 我尝试了这个,它似乎只影响第一个复选框,但无论如何再次点击它都不会取消选中复选框... 我还看到了其他一些做法,但我不确定它们是否完全符合Rails(或任何应该使用的术语)。
所以,请有人指点一下我正确的方向吗?
以下是我的视图:
<%= render 'admin/distributions/head' %>
<% title 'Workflow' %>


<%= form_for @search, :url => url_for(:controller => params[:controller], :action => params[:action]), :html => {id => "distribution_workflow",:method => :get} do |f| %>

  <div class="opportunity-block yellow">

    <div class="form-block mrl mbm">
      <%= f.label :created_at_gt, "Created at >" %>
      <%= f.text_field :created_at_gt, :class => "js-db-date-picker" %>
    </div>

    <div class="form-block mrl mbm">
      <%= f.label :created_at_lte, "Created at <=" %>
      <%= f.text_field :created_at_lte, :class => "js-db-date-picker" %>
    </div>

    <div class="form-block mrl mbm mtm">
      <%= f.label :status_equal, "Status" %>
      <%= f.select :status_equal, %w(delivered no_success already_registered qa_complete success follow_up), :include_blank => " " %>
    </div>

    <div class="clear"></div>
    <%= submit_tag 'Apply Filter', :class => "input-button dark unit-right mrl" %>
    <div class="clear"></div>
  </div>
<% end  %>


<%= form_tag edit_multiple_admin_distributions_workflows_path , :id => "workflow_form" do %>
<%= submit_tag "Edit Selected" %>
  <table class="standard-grid"> 
    <tr> 
      <th class="first"> </th>
      <th>ID</th>
      <th>Customer</th>
      <th>Resume URL</th>
      <th>Partner</th>
      <th>Status</th>
      <th>Assigned To</th>
      <th>Comments</th>
    </tr>
    <% @report.each do |distribution| %>
      <tr>
        <td><%= check_box_tag "distribution_ids[]", distribution.id %></td>
        <td>
          <%= distribution.owner.id %>
        </td>
        <td>
          <%=link_to distribution.owner.full_name, "mailto:#{distribution.owner.email}" %>
        </td>
        <td>
          <a href=<% UrlService.download_blob_url(distribution.resume) %>>Resume URL</a>
        </td>
        <td>
          <%=link_to distribution.matching_profile.partner.title,  "mailto:#{distribution.matching_profile.partner.email}" %>
        </td>
        <td>
          <%= distribution.status %>
        </td>
        <td>
          <%= distribution.assignee_id ? User.find(distribution.assignee_id).full_name : " " %>
        </td>
        <td>
          <%= distribution.comments %>
        </td>
      </tr>
    <% end %>
  </table>
<% end %>

我不确定我理解这个问题与Rails有什么关系。这通常是一个Javascript问题,需要您发布呈现的HTML并重新标记问题。 - iwasrobbed
我应该发布整个页面的所有HTML吗?还是仅发布表单的HTML就足够了? - Ramy
我尽力在下面回答你的问题。以后参考,如果涉及在页面呈现后更改页面,则可能是Javascript(除非您呈现.js.erb文件或类似文件)。 - iwasrobbed
4个回答

22

以下是一个可用的示例:http://jsfiddle.net/wYPWL/

HTML 示例:

<input type="checkbox" id="selectAll" value="selectAll"> Select / Deselect All<br/><br/>

<input type="checkbox" name="foo" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo" value="bar4"> Bar 4<br/>

Javascript:

$('#selectAll').click(function() {
   if (this.checked) {
       $(':checkbox').each(function() {
           this.checked = true;                        
       });
   } else {
      $(':checkbox').each(function() {
           this.checked = false;                        
       });
   } 
});
无论你的复选框命名为何,以下代码都可以生效。如果你确实只想针对在你的代码中显示的复选框,请将 $(':checkbox') 替换为 $('input[id^="distribution_ids"]') ,这是jQuery用于定位具有以distribution_ids 开头的ID的输入元素的方法。

太棒了!正是我想要的。我对Rails、Web开发和JavaScript都很陌生。非常感谢你! - Ramy
1
很高兴能帮忙,我猜想你可能是前端方面的新手 :) jQuery 就是你的好伙伴,相信我。如果你的 rails 项目中还没有安装它,可以参考这个链接:https://github.com/rails/jquery-ujs - iwasrobbed
2
太好了!我整天都在找这个!谢谢。 - Desingh
@iWasRobbed:抱歉,我在您的答案中发现了一个问题...尝试选择全部,然后取消勾选任何一个选项(如Bar1、Bar2、Bar3)..."选择全部"必须取消勾选...请检查我的答案。 - Gagan Gami

2
如果使用jquery,你可以使用以下代码(使用coffeeScript):
if (this.checked)
  $(':checkbox').each ->
    $(this).prop('checked', true)                     
else
  $(':checkbox').each ->
    $(this).prop('checked', false)

我发现尝试设置this.checked = false时出现了问题 - 不太确定发生了什么,但上面的代码可行。


1
我发现了一个问题,关于 iWasRobbed 的答案。如果选中了 Select All,然后取消选中其中的任何一个选项,例如 (Bar1, Bar2, Bar3),那么 Select All 必须被取消选中...
这里是解决方案。 HTML 代码
<input id="campaign_range_ids_1" class="checkbox" type="checkbox" value="1" name="campaign[range_ids][]"> India
<input id="campaign_range_ids_2" class="checkbox" type="checkbox" value="2" name="campaign[range_ids][]"> London
<input id="campaign_range_ids_3" class="checkbox" type="checkbox" value="3" name="campaign[range_ids][]"> USA
<input id="campaign_range_ids_4" class="checkbox" type="checkbox" value="4" name="campaign[range_ids][]"> All

JavaScript 代码:
$('#campaign_range_ids_4').click(function () {

    if ($(this).is(':checked')) {
        $('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').prop('checked', true);

    } else {
        $('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').prop('checked', false);
    }

});

$('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').click(function () {

    if ($(this).is(':checked')) {
        } else {
            $('#campaign_range_ids_4').prop('checked', false);
    }

});

工作演示


0

如果你想在同一页上选择多个单独的列表,可以使用以下方法:

$("input[data-select-all]").on("click", function() {
    let $checkboxes = $("input[data-"+$(this).data("select-all")+"]");
    if (this.checked) {
        $checkboxes.each(function() {
            this.checked = true;
        });
    } else {
        $checkboxes.each(function() {
            this.checked = false;
        });
    }
});


<table>
  <thead>
  <tr>
    <th><input type="checkbox" name="selected" id="select-all" value="1" title="Select all" data-select-all="select-all-target"></th>
    <th>Asset</th>
    <th>Description</th>
  </tr>
  </thead>
  <tbody>
    <tr>
    <td><span class="form-check"><input type="checkbox" name="selected" id="asset_type_11" value="1" data-select-all-target="true"></span></td>
    <td>The name</td>
    <td>The description</td>
    </tr>
  </tbody>
</table>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接