我想根据选择值生成一些复选框。所以我有选择标签:
<%= f.collection_select :type, RequestType.order(:typeName), :id, :typeName,
{include_blank:true }, {:class => "types"} %>
当 select 的值发生变化时,我想生成一些复选框,为此我有 div:
<div id="sub_types"> </div>
def show_sub_types
@rtype = params[:id];
@stypes = RequestSubType.where("request_type_id=?", @rtype).all
respond_to do |format|
format.js
format.html
end
end
$("#sub_types").html("");
$("#sub_types").append("<%= j render 'show_sub_types', stypes: @stypes %>");
<% stypes.each do |type| %>
<%= check_box_tag "subtype", type.id %>
<%= type.subTypeName %>
<br>
<% end %>
在我的部分,我做了这样的事情。 这段代码生成了我的复选框。它们看起来像这样:
<input type="checkbox" name="subtype" id="subtype" value="1">
但现在我不知道如何使用表单提交这些复选框值。我想将多个复选框值作为数组存储在数据库中。
最佳答案
请尝试这个
<% stypes.each do |type| %>
<%= check_box_tag 'stype_ids[]', type.id %>
<%= type.subTypeName %>
<br>
<% end %>
关于javascript - 在 Rails 4 中生成复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344377/