php原生select获取后台数据,并展示在前端
$(function () {
$("#seekMainEquipment").empty();
$.ajax({
url: "http://www.equipment.com/seekMainEquipment.php/",
type: "post",
dataType: "json",
success: function (data) {
var admin = data;
console.log(admin);
//定义一个空字符串来拼接循环的内容,最后整体添加。
var obj ='';
obj +="<option class='option' value='"+ 0+"'>"+ '请选择选项' + "</option>";
for (var dict = 0; dict < admin.length; dict++) {
obj += "<option class='option' value='"+ admin[dict].id +"'>"+ admin[dict].name + "</option>";
}
AppenNode(obj);
//下拉渲染必需写
function AppenNode(value, text){
$("#seekMainEquipment").append("<option value='"+ value +"'>"+ text +"</option>");
}
}
})
});
html:
<select class="main-select" name="seekMainEquipment" id="seekMainEquipment">
</select>