Rails 3.1里选择全部复选框 [英] Rails 3.1 Select All checkbox
问题描述
这是我的看法:
@bulk_objects.each do |bulk_warehouse|
bulk_error = @wh_errors[:"bulk_warehouse#{@count}"] if @wh_errors
-%>
<tr class="<%= cycle("odd", "even") %>">
<%= hidden_field_tag("bulk_warehouse_id#{@count}",bulk_warehouse.id) %>
<td><%= text_field_tag("bulk_warehouse_asset#{@count}", bulk_warehouse.asset, :disabled => true)%></td>
<td><%= text_field_tag("bulk_warehouse_serial#{@count}", bulk_warehouse.serial, :disabled => true) %></td>
<td><%= check_box_tag "enable_record#{@count}",1,false,{:onclick => "bulk_warehouse_asset#{@count}.disabled =
bulk_warehouse_serial#{@count}.disabled =
!this.checked;"}%></td>
<td class="last">
<%= link_to "#{t("web-app-theme.delete", :default => "Delete")}", bulk_warehouse_path(bulk_warehouse), :method => :delete, :confirm => "#{t("web-app-theme.confirm", :default => "Are you sure?")}" %>
</td>
</tr>
</div>
<% @count = @count +1 %>
<% end -%>
</table>
<div class="actions-bar wat-cf">
<div class="actions">
</div>
..
这是我的控制器:
and this is my controller:
@bulk_objects = BulkWarehouse.all
@count= @bulk_objects.count
现在我会在我的视图中添加一个全选复选框,当你点击它使所有其他enable_record#{@计}复选框。我知道应该做这件事使用Ajax和jQuery,但我不知道怎么办。任何人都可以帮我吗? 谢谢你会
Now I would add in my view a "Select all" checkbox that when you click on it enables all other "enable_record#{@count}" checkbox. I know that this thing should be done using Ajax and Jquery but I don't know how. Anyone can help me? Thank you ll
推荐答案
您可以使用button_to_function创建一个按钮,将选中/取消所有方框的形式。
You can use button_to_function to create a button that will check/uncheck all the boxes on the form.
把这个code。在您看来,您希望您的选中/清除按钮即可进入。当单击该按钮,它将运行toggleChecked JavaScript函数。
Place this code in your view, where you want your check/uncheck button to go. When the button is clicked, it will run the toggleChecked javascript function.
<%= button_to_function "Check / Uncheck All", "toggleChecked()" >
将您的javascript code在视图的底部。
Place your javascript code at the bottom of your view.
<script type='text/javascript'>
var toggleChecked = function() { $('input[type="checkbox"]').click(); });
</script>
该功能也可投入资产/ JavaScript的相应.js文件。
The function also can be put into the respective .js file in assets/javascripts.
这应该让你和运行。
Button_to_function现在是很precated。我建议使用HTML5的按钮和jQuery这个处理。
Button_to_function is now deprecated. I recommend handling this using an HTML5 button and jQuery.
查看$ C $下您的按钮:
View code for your button:
<button type="button" id="check_all">
Check / Uncheck All
</button>
JavaScript的:
Javascript:
<script type='text/javascript'>
$('#check_all').on("click", function(){ $('input[type="checkbox"]').click(); });
</script>
这篇关于Rails 3.1里选择全部复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!