简体   繁体   English

我如何重新填充jQuery中的下拉列表

[英]How can i repopulate the dropdown list in jquery

I'm trying to make a dropdown list that when one item is selected then the other dropdown will change it's list 我正在尝试创建一个下拉列表,当选择一项时,另一个下拉列表将更改它的列表

So fart this is what i've done, 放屁,这就是我所做的
This first dropdown that will change the list of another dropdown: 第一个下拉列表将更改另一个下拉列表的列表:

<select id="testers_team-${info.problemId}" class="tester_team">
    <optgroup label="Current Value">
         <option><c:out value="${info.team}"></c:out></option>
    </optgroup>
    <optgroup label="Teams">
         <c:forEach var="team" items="${requestScope.testers}">
        <option value="${team.key}">${team.key}</option>
         </c:forEach>
    </optgroup>
</select>

This is the second dropdown that will be change if another dropdown is selected: 这是第二个下拉列表,如果选择了另一个下拉列表, 则将更改

<select id="testers" class="testers_team-${info.problemId}">
    <optgroup label="Current Assigned Tester">
        <option>I'll think about this later</option>
    </optgroup>
    <optgroup label="Testers">
          <c:forEach var="testers_per_team" items='${test.KwekKwek}'>
               <option>${testers_per_team}</option>
          </c:forEach>
    </optgroup>
</select>

This code is the Ajax that get the proper value and send it back that should be repopulating the dropdown: 这段代码是获得正确值并将其发送回应该重新填充下拉列表的Ajax

$('.tester_team').each(function(){
             $(this).change(function() {
                 var id = $(this).attr('id');

                  $.post('Analysis',
                     {team: $(this).val()},
                     function(data)
                     {
                        $('.'+id).each(function()
                        {
                            $(this).find('option').remove();
                            $.each(data, function(index, value) {              
                                $('<option>').text(value).appendTo($select);
                            });
                        });

                     },"json"); 
             }); 
         });

What is done already is: 已经完成的工作是:

  1. Send to the servlet the selected value of the first dropdown 将第一个下拉列表的选定值发送到servlet
  2. Process the value so it will return the proper value to repopulate the second dropdown 处理该值,以便它将返回正确的值以重新填充第二个下拉列表
  3. Get the value in JSON form JSON形式获取值
  4. Removed the list from the SECOND dropdown when another value is selected on the FIRST dropdown 在FIRST下拉菜单中选择了另一个值后,从SECOND下拉列表中删除了该列表

The problem is the repopulating itself, is used 问题是重新填充本身,用于

$.each(data, function(index, value) {              
    $('<option>').text(value).appendTo($select);
});

to repopulate the list of the SECOND dropdown but it seems that code is wrong. 重新填充第二个下拉列表的列表,但似乎代码是错误的。 Please help me to resolve this. 请帮助我解决此问题。

NOTE : Both dropdown is iterated by foreach thats why is used the id of the first dropdown to identify the second dropdown relative to it. 注意 :两个下拉列表都通过foreach迭代,这就是为什么使用第一个下拉列表的id来标识相对于它的第二个下拉列表。 And about the data that send back by the servlet, it is not null, i tried to pop it out in an alert(); 关于由servlet返回的数据,它不为null,我试图将其弹出到alert(); and it is there. 它在那里。

Can you try this ? 你可以试试这个吗?

$('.'+id).each(function()
  {
    $(this).find('option').remove();
    var optgroup = $("optgroup[label=Testers]", this);
    $.each(data, function(index, value) {              
      $('<option>').text(value).appendTo(optgroup);
    });
  })

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

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