jQuery发送ajax填充表单的写法
这里记录一下以便后续使用:
=====================================================================
填充下拉框
html:
<div class='col-md-10'>
<select name="ethnic" id="ethnic" lay-verify="">
<option value="">选择民族</option>
</select>
</div>
js:
/*获取民族*/
function getMz() {
$.ajax({
"url": "/XXXXXXXXXX",
"type": "get",
async: false,
"success": function (data) {
var mz = data.data;
for (var i = 0; i < mz.length; i++) {
var code = mz[i].code;
var name = mz[i].name;
$("#ethnic").append("<option value='" + code + "'>" + name + "</option>")
}
}
})
}
填充checkbox
html:
<div class='form-group'>
<label class='col-md-2 control-label'>“XX”情况</label>
<div class='col-md-10' id='sasqk'>
<div hidden>
<input style="display:none" type='checkbox' name='sasqk' lay-filter='sasqk' value="sasqk" checked>
<input style="display:none" type='checkbox' name='sasqk' lay-filter='sasqk' value="sasqk" checked>
</div>
</div>
</div>
注:上面代码中两个input是为了layui填充数据所设置的与填充checkbox无关
js:
/**
* 获取XXXX情况字典值
*/
function getSasqk() {
var element = $("#sasqk").last();
$.ajax({
"url": "/XXXXXXXXXX",
"type": "get",
async: false,
"success": function (data) {
var dut = data.data;
if (data.code == 0) {
for (var i = 0; i < dut.length; i++) {
var k = dut[i].k;
var val = dut[i].val;
element.append("<input type='checkbox' name='sasqk' title=" + val + " value=" + k + ">")
}
} else {
alert(data.message);
}
}
})
}