Javascript 全选/取消选中页面中的所有复选框
我已经看过了,它似乎对我的代码没有影响。 我试过了,它似乎只影响第一个复选框,但当我再次单击它时,它不会取消选中复选框。。。。 我还看到了一些其他的方法,我不确定这些方法是否完全是Rails风格的(或者不管术语应该是什么) 那么,有人能给我指一下正确的方向吗 以下是我的看法:Javascript 全选/取消选中页面中的所有复选框,javascript,ruby-on-rails-3,forms,checkbox,Javascript,Ruby On Rails 3,Forms,Checkbox,我已经看过了,它似乎对我的代码没有影响。 我试过了,它似乎只影响第一个复选框,但当我再次单击它时,它不会取消选中复选框。。。。 我还看到了一些其他的方法,我不确定这些方法是否完全是Rails风格的(或者不管术语应该是什么) 那么,有人能给我指一下正确的方向吗 以下是我的看法: <%= render 'admin/distributions/head' %> <% title 'Workflow' %> <%= form_for @search, :url =&g
<%= 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 %>
(:controller=>params[:controller],:action=>params[:action]),:html=>{id=>“分发工作流程”,:method=>:get}do | f |%>
“js db日期选择器”%>
“js db日期选择器”%>
" " %>
“输入按钮暗单位右mrl”%>
“工作流表单”do%>
身份证件
顾客
恢复URL
搭档
地位
分配给
评论
以下是一个工作示例:
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/>
无论复选框的名称如何,这都将起作用。如果您真的只想针对上面代码中显示的复选框,可以将
$(':checkbox')
替换为$('input[id^=“distribution\u ids”]”)
,这是jQuery针对id以distribution\u id开头的输入元素的方法。如果使用jQuery,您可以使用以下方法(咖啡脚本):
我在尝试设置时发现一个问题。checked=false-不确定发生了什么,但上面的代码起了作用。我发现的答案有一个问题,即如果选中了
全选,然后取消选中任何一个选项,如(Bar1
,Bar2
,Bar3
)然后必须取消选中“全部选择”
这是解决办法
HTML代码
如果要为同一页面上的多个单独列表选择“全部”
$("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>
$(“输入[数据选择全部]”)。在(“单击”,函数()上){
设$复选框=$(“输入[数据-”+$(this).data(“全选”)+“]);
如果(选中此项){
$复选框。每个(函数(){
this.checked=true;
});
}否则{
$复选框。每个(函数(){
此项检查=错误;
});
}
});
资产
描述
名字
描述
我不确定我是否理解这个问题与Rails有什么关系。这通常是一个Javascript问题,需要您发布呈现的HTML并重新标记问题。我应该发布整个页面的所有HTML吗?或者表单的HTML是否足够?我试着尽我所能回答您的问题。对于未来的r请参阅,如果在呈现页面后需要更改页面,则可能是Javascript(除非您正在呈现.js.erb
文件或类似文件)太棒了!这正是我想要的。我对rails、web开发和javascript都是新手。真是太感谢你了!很高兴为你提供帮助,我觉得你对事物的前端是新手:)相信我,jQuery是你的朋友。如果您还没有在rails项目中安装它,请参阅以下链接:非常好!我找了一整天!谢谢。@Iwasrobed:对不起,老兄,我在你的回答中发现并提出了。。。尝试选择全部,然后取消选中任何一个选项(如Bar1、Bar2、Bar3)。。“全选”必须取消选中。。。检查我的答案。。
if (this.checked)
$(':checkbox').each ->
$(this).prop('checked', true)
else
$(':checkbox').each ->
$(this).prop('checked', false)
<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
$('#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);
}
});
$("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>