我用的是form.on监听事件,不多说了,直接代码
<div class="layui-form-item">
<label class="layui-form-label">产品名称</label>
<div class="layui-input-block">
<select id="contents" name="content" lay-filter="content">
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">模具制号</label>
<div class="layui-input-block">
<select id="no" name="no" lay-filter="no">
</select>
</div>
</div>
这是两个select框,根据下面这个的选择上面那个自动选择
//简单的ajax
$.ajax({
url: WEB_CONTEXT+'rest/proWorkRestFul/getList',
type: "post",
dataType : "json",
contentType : "application/json",
async: false,
success: function (result) {
workData = result.data;
for(var x in workData){
strs += '<option value = "' + workData[x].no + '">' + workData[x].no + '</option>'
}
$("#no").html(strs);
}
});
//添加layui的监听事件
form.on('select(no)', function(data){ //no是那个lay-filter的值
var nos=$("#no").val();//获得选中的option的值
console.log(nos);//没啥用
$.ajax({
url: WEB_CONTEXT+'rest/proWorkRestFul/getListByCondition?no='+nos,//带参数将这个传到后台条件查询方法里面
type: "post",
dataType : "json",
contentType : "application/json",
async: false,//同步
success: function (result) {
var data = result.data;
console.log(data);
for(var x in data){
str += '<option value = "' + data[x].id + '">' + data[x].content + '</option>'
}
$("#contents").html(str);
}
});
form.render('select');//最后记得渲染
});