Javascript Rails:使用Jquery更改简单表单输入值
我是rails的新手,已经尝试了几个小时寻找答案,但仍然没有运气。 我创建了一个“全选”按钮,这样一旦单击它,区域就会自动填充所有区域代码。 提前感谢您的帮助 部分形式:Javascript Rails:使用Jquery更改简单表单输入值,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我是rails的新手,已经尝试了几个小时寻找答案,但仍然没有运气。 我创建了一个“全选”按钮,这样一旦单击它,区域就会自动填充所有区域代码。 提前感谢您的帮助 部分形式: <div id = "select_regions_form"> <%= render partial: 'reports/regions_form', locals: { f: f, all_regions: @all_regions } %> <%= button_tag "Sele
<div id = "select_regions_form">
<%= render partial: 'reports/regions_form', locals: { f: f, all_regions: @all_regions } %>
<%= button_tag "Select All", :id => "select_all", :class => "btn btn-small btn-inverse", :type => "button" %>
</br>
</div>
parial\u region\u form.html.erb: 将ID添加到选择输入或使用rails自动创建的现有ID(仅供参考:您可以在浏览器中查看该ID) 这里,我为select字段添加了一个ID
select\u regions
<%= f.input :regions, collection: all_regions,
label_method: :region_code, value_method: :id, input_html: { multiple: true, id: 'select_regions' }%>
嗨,当我点击你有脚本的文件中的按钮时,这仍然没有任何作用。在单独的JS文件中还是以部分形式?
(function($){
"use strict";
$(document).ready(function(){
$("#select_all").click(function() {
$("input:regions").val(*all the region codes*);
} );
} );
})(jQuery);
<%= f.input :regions, collection: all_regions,
label_method: :region_code, value_method: :id, input_html: { multiple: true, id: 'select_regions' }%>
$(document).ready(function(){
$("#select_all").click(function() {
$('#select_regions option').prop('selected', true);
});
});