简体   繁体   English

多个下拉列表的相关下拉选项

[英]Dependent dropdown options for multiple dropdowns

I have a simple jQuery working to populate second dropdown options based the selection of the first dropdown option. 我有一个简单的jQuery工作,根据第一个下拉选项的选择填充第二个下拉选项。 But I need to have multiple instances of this dependency in rows. 但是我需要在行中有多个这种依赖的实例。

It works the first time, but if I change the first dropdown option in another row, all of the second options previously selected gets reset. 它第一次工作,但如果我更改另一行中的第一个下拉选项,则先前选择的所有第二个选项都会重置。

I need help with how to retain the second dropdown options when another row of first dropdown option is changed. 我需要有关如何在第一个下拉选项的另一行更改时保留第二个下拉选项的帮助。 Here is my code so far, 这是我的代码到目前为止,

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

<script type="text/javascript">

dairy=new Array('Select','Milk','Egg','Cheese');
fruits=new Array('Select','Apple','Orange','Banana');

populateSelect();

$(function() {

  for( var i = 0; i < 7; i++ ) {

  $('#diet'+i).change(function(){
    populateSelect();
     });
    }
  });

function populateSelect(){

     for( var i = 0; i < 7; i++ ) {

     type=$('#diet'+i).val();
     $('#item'+i).html('');

     if(type==1){
         dairy.forEach(function(t) { 
         $('#item'+i).append('<option>'+t+'</option>');
       });
      }    

      if(type==2){
          fruits.forEach(function(t) { 
          $('#item'+i).append('<option>'+t+'</option>');
       });
      }
    }
   } 

</script>

And here is the HTML portion, 这是HTML部分,

<table class="report-table">
<tr>
    <th>Days</th>
    <th>Diet</th>
    <th>Item</th>
   </tr>
<tr>
  <td>Mon</td><td align='center'><select name="diet0" id="diet0">
  <option value='' selected>Select Diet</option>
  <option value="1">Dairy</option>
  <option value="2">Fruit</option></select></td>
  <td align='center'><select name="item0" id="item0">
  <option value='' selected>Item...</option>
  </select></td>
 </tr><tr>
  <td>Tues</td><td align='center'><select name="diet1" id="diet1">
    <option value='' selected>Select Diet</option>
    <option value="1">Dairy</option>
    <option value="2">Fruit</option>
    </select></td>
  <td align='center'><select name="item1" id="item1">
    <option value='' selected>Item...</option>
  </select></td>
  </tr><tr>
    <td>Wed</td><td align='center'><select name="diet2" id="diet2">
      <option value='' selected>Select Diet</option>
      <option value="1">Dairy</option>
      <option value="2">Fruit</option>
      </select></td>
    <td align='center'><select name="item2" id="item2">
      <option value='' selected>Item...</option>
    </select></td>
    </tr><tr>
      <td>Thurs</td><td align='center'><select name="diet3" id="diet3">
        <option value='' selected>Select Diet</option>
        <option value="1">Dairy</option>
        <option value="2">Fruit</option>
        </select></td>
      <td align='center'><select name="item3" id="item3">
        <option value='' selected>Item...</option>
      </select></td>
      </tr><tr>
        <td>Fri</td><td align='center'><select name="diet4" id="diet4">
          <option value='' selected>Select Diet</option>
          <option value="1">Dairy</option>
          <option value="2">Fruit</option>
          </select></td>
        <td align='center'><select name="item4" id="item4">
          <option value='' selected>Item...</option>
        </select></td>
        </tr><tr>
          <td>Sat</td><td align='center'><select name="diet5" id="diet5">
            <option value='' selected>Select Diet</option>
            <option value="1">Dairy</option>
            <option value="2">Fruit</option>
            </select></td>
          <td align='center'><select name="item5" id="item5">
            <option value='' selected>Item...</option>
          </select></td>
          </tr><tr>
            <td>Sun</td><td align='center'><select name="diet6" id="diet6">
              <option value='' selected>Select Diet</option>
              <option value="1">Dairy</option>
              <option value="2">Fruit</option>
              </select></td>
            <td align='center'><select name="item6" id="item6">
              <option value='' selected>Item...</option>
            </select></td>
            </tr>
</table>

I have this posted on JSFiddle, so you can see what it's doing. 我在JSFiddle上发布了这个,所以你可以看到它在做什么。 https://jsfiddle.net/sn4hjrLd/ https://jsfiddle.net/sn4hjrLd/

I appreciate any help, Cheers! 我很感激任何帮助,干杯!

In your change handler, you are cycling through all selects and updating them all. 在您的更改处理程序中,您将遍历所有选择并更新它们。

You should assign class names to all selects ("diet" and "item"). 您应该为所有选择(“节食”和“项目”)分配类名。 Then on your change handler you can do 然后在你的改变处理程序上你可以做

var item = $(this).closest('tr').find('.item');
item.html("");
//loop through array and populate item.

With these changes, your on change handler works only within a specific row. 通过这些更改,您的on change处理程序仅在特定行中有效。

To further expand, you can place your change handler on the table and not deal with the loops or ids at all. 要进一步扩展,您可以将更改处理程序放在表上,而不是处理循环或ID。

Here is an update to your fiddle to show a single change handler and row-relative updates: https://jsfiddle.net/sn4hjrLd/1/ 这是对您的小提示的更新,以显示单个更改处理程序和行相关更新: https//jsfiddle.net/sn4hjrLd/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM