jQuery:迭代一个ajax响应列表并在下拉列表中填充它 [英] jQuery: Iterating a ajax response list and populating and it in dropdown list

查看:100
本文介绍了jQuery:迭代一个ajax响应列表并在下拉列表中填充它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自servlet的JSON响应

JSON response from servlet as

empID: rajuM, givenName: Raju M, empID: rajuM01, givenName: Raju M R

我要遍历此列表并将其填充在下拉列表中.我已经尽力做到了,但是没有运气请帮忙.请查看下面的代码.

I want to iterate this list and populate it in the drop down list . I have tried my best to do it but no luck please help. Please look at the code below.

$('#select').change(function(){
    var dept = $('select[name="select-dept"] option:selected').text();
    console.log(dept);
    $.ajax({
        type: "GET",
        url: "updateEmployeeServlet",
        data: {dept: dept},
        success: function(list){
        $('#employee-list').html("Please select Employee under Department:      <select name = \"employee\"></select>");

            $.each(list, function(index, data){
                $('#employee-list select').html("<option value = "+data.empID+">"+data.givenName+"</option>");
            });
        },
        error: function(){
            alert("something went wrong");
        }
    });
});

推荐答案

您可以这样做:

首先,您需要以下数据对象: [{'empID':'rajuM','givenName':'Raju M'},{'empID':'rajuM01','givenName':'Raju M R '}]

success: function(list)
{
  $('#employee-list').html("Please select Employee under Department:<select name =\"employee\"></select>");
  var options = "";
  for(i in list)
  {
     options += "<option value = "+list[i].empID+">"+list[i].givenName+"</option>"; 
  }
  $('#employee-list select').append(options);
}

这里是小提琴.

这篇关于jQuery:迭代一个ajax响应列表并在下拉列表中填充它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆