0

How can I have a collection_select with 5 options to chose from and when either one is chosen it cannot be picked again for the next (ie if 1 is picked first then only 2,3,4,5 are available to pick from).

我有两个表:invoices 和 Invoice_Line_Items,invoice has_many Invoice_Line_items。Invoice_Line_Items 属于_to Invoice。选项是关系。例如,我不能为同一项目开两次发票。

<%= collection_select( :invoice_line_item, :invoice_line_item_id, @invoice_line_items, :id, :supplier_id, {}, {:multiple => true}) %>

谢谢

4

1 回答 1

1

为此,您需要一些 javascript。下面的示例仅基于两个下拉菜单,第一个的值一旦被选择就会从第二个选项列表中删除,但您应该能够根据您需要的数量进行调整。注意“collect_2.remove(collect_1.value - 1)”部分——这是因为下拉对象的计数从 0 开始,但它们的值从 1 开始——有点混乱!

<!DOCTYPE html>
<html>
<body>

<%= form_tag("/users", method: "get", name:"frm") do %>  
    <%= collection_select(:invoice_line_item, :invoice_line_item_id, @invoice_line_items, :id, :supplier_id, {},{onchange: "myFunction()",id:"collect-1", multiple: true }) %><br>
    <%= collection_select(:invoice_line_item, :invoice_line_item_id, @invoice_line_items, :id, :supplier_id, {},{id:"collect-2", multiple: true}) %><br>   
  <%= submit_tag("Go") %>
<% end %>

<script>
function myFunction() {  
    var collect_1 = document.getElementById("collect-1");
    var collect_2 = document.getElementById("collect-2"); 
    collect_2.innerHTML = collect_1.innerHTML;
    collect_2.remove(collect_1.value - 1);    
}   
</script>

</body>
于 2015-04-15T10:05:29.123 回答