【发布时间】:2015-04-28 13:06:13
【问题描述】:
我想使用 ajax 调用动态填充下拉列表文本和值以检索 JSONObject。 我的代码正确填充了下拉列表,但我希望该值与文本不同,因为我想将 id 保存在我的数据库中而不是名称中。
这是我的代码:
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}
谁有解决办法?
【问题讨论】:
-
可能使用隐藏输入。
标签: javascript jquery ajax json jsp