如何使用jquery迭代动态生成的组合框

如何使用jquery迭代动态生成的组合框,jquery,ruby-on-rails,erb,Jquery,Ruby On Rails,Erb,我正在使用html.erb在rails应用程序中生成视图 我使用“each do | |” 如何使用jquery遍历动态生成的组合框的值,并在组合框的任何值相同时向用户发出警告 <table class="holiday_allowance_schedule tabular_data_cell"> <thead> <th class="years_from_accrual_start"> <%= a(:company_po

我正在使用html.erb在rails应用程序中生成视图

我使用
“each do | |”

如何使用jquery遍历动态生成的组合框的值,并在组合框的任何值相同时向用户发出警告

    <table class="holiday_allowance_schedule tabular_data_cell">
  <thead>
    <th class="years_from_accrual_start">
      <%= a(:company_policy, :years_from_accrual_start) %>
    </th>
    <th class="months_allocated"><%= a(:company_policy, :days_allocated) %></th>
    <th></th>
    <th></th>
  </thead>
  <tbody id="schedule_table">
    <% @policies.holiday_allocation_schedule.keys.sort.each do |year| -%>
      <tr id="<%= year %>">
        <% days = @policies.holiday_allocation_schedule[year] %>

        <td><%= select_tag("holiday_schedule[][year]", options_for_select(holiday_allocation_years_options, year)) %></td> #combo boxes with years

        <td><%= select_tag("holiday_schedule[][days]", options_for_select(holiday_allocation_days_options, days)) %></td>
        <td class="actions"><%= add_icon %></td>
        <td class="actions"><%= remove_icon %></td>
      </tr>
    <% end -%>
  </tbody>
</table>

#带年份的组合框
例如:有五个组合框

一年

两年

三年

4年

五年

如果用户在任意两个或更多组合框中选择“3年”,则必须显示警报

如何使用jquery或使用erb本身来实现这一点

将change()监听器绑定到所有组合框,请在其中进行检查

$('select').change(function(){
   var values = []
   $.each($('select'), function(idx, sel){
     if(values.index(sel.val()) != -1){
       alert('same value selected')
     }else{
       values.push(sel.val())
     }
   })
})

请在此答案中添加其他信息,例如一些示例代码。抱歉,nickcen,我无法理解您的回答answer@YasirAzzu逻辑如下。您应该将change()侦听器绑定到所有select输入。当任何select输入值发生更改时,将调用侦听器。监听器内部的逻辑是返回所有select的值,并查看是否有两个select具有相同的值。谢谢nickcen,我理解了你的答案。它成功了,我已经解决了我的问题。