通过ajax访问后台,获取json对象,并赋值到select当中。
后台返回的json对象如下:(以获取快递公司为例)
[{"code":"debangwuliu","name":"德邦物},
{code":"shunfeng","name":"顺丰速递"},
{"code":"yuantong","name":"圆通速递"},
{"code":"yunda","name":"韵达快运"},
{"code":"yuntongkuaidi","name":"运通快递"},
{"code":"zhongtong","name":"中通速递"},
{"code":"zhaijisong","name":"宅急送"}
]
html代码:
<select id="expressCompany">
<option value="0"></option>
</select>
//获取快递公司
function getExpressCompany() {
$.ajax({
type : "post",
url : url, //url根据自己的项目实际定义
async : false,
dataType : "text",
error : function() {
alert("系统忙,请稍后再试!");
return false;
},
success : function(text) {
var expressCompany = $("#expressCompany");
var str = '';
var data = eval("("+text+")");;
for(var o in data) {
str += '<option value="'+data[o].code+'">'+data[o].name+'</option>';
}
expressCompany.append(str);
}
});
}