簡體   English   中英

獲取所有選中的復選框值

[英]Get all checked checkboxes values

我有以下形式:

<%= form_for([company,service], :remote => true) do |f| %>
    <%= health_care_insurances.each do |hc_insurance| %>
        <label><%= hc_insurance.name %></label>
        <input id="health_care_insurance_<%= user.id %>" name="<%= hc_insurance.id %>" value="<%= hc_insurance.id %>" type="checkbox"><br/>
    <% end %>
    ...
<% end %>

提交后,為了獲得良好的性能,我想獲取一個包含所有已檢查的“ hc_insurance.ids”的數組。 我知道我可能會使用javascript將它們添加到hidden_​​field,然后從那里拾取它們,但是我不確定這是個好方法。

是否有任何方法可以從控制器中拾取它們?

使復選框的所有名稱與數組框相同,如下所示(這只會將選中的值提交到后端)

<input id="health_care_insurance_<%= user.id %>" name="health_care_insurence_ids[]" value="<%= hc_insurance.id %>" type="checkbox"><br/>

要獲取控制器中所有health_care_insurence的ID(假設health_care_insurence是單個模型)。

HealthCareInsurence.pluck(:id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM