2015-04-28 65 views
-1

我想使用ajax调用来检索JSONObject动态填充下拉列表文本和值。 我的代码正确填充下拉列表,但我希望该值与文本不同,因为我想将该ID保存在数据库中而不是名称中。使用ajax json调用动态填充下拉值和文本

这是我的代码:

  function get_landlords(){ 
     $.ajax({ 
      url: "get_landlord_list.jsp", 
      dataType: "json", 
      success: function(data){ 
       var options, index, select, option; 
       select = document.getElementById("landlord"); 


       select.options.length = 0; 
       options = data.data; 

       for(index = 0; index < options.length; ++index){ 
        option = options[index]; 
        if(option.company_name !== null){ 
         select.options.add(new Option(option.landlord)); 
        } 
       } 
      } 
     }); 
    } 

<select name="landlord" id="landlord" class="form-control" onfocus="javascript:get_landlords();"> 
<option value="-1" name="-" selected>-</option> 
</select> 

我的JSON是如下:

0: {landlord: "John Doe", id: 6} 
1: {landlord: "John Doe", id: 4} 
2: {landlord: "John Doe", id: 1} 
3: {landlord: "John Doe", id: 3} 

任何人都有一个解决方案吗?

+0

也许使用一个隐藏的输入。 – brso05

回答