87,903
社区成员
发帖
与我相关
我的任务
分享
<tr>
<td>战略性新兴产业:</td>
<td colspan="3">
<div class="width180 float-l">
<select name="newIndustry" id="newIndustry" lay-filter="newIndustry">
<option value="">--请选择--</option>
<option value="newIndustry1" ${project.newIndustry=="newIndustry1" ? "selected" : ""}>新一代信息技术产业</option>
<option value="newIndustry2" ${project.newIndustry=="newIndustry2" ? "selected" : ""}>高端装备制造产业</option>
<option value="newIndustry3" ${project.newIndustry=="newIndustry3" ? "selected" : ""}>新材料产业</option>
</select>
</div>
<div class="width180 float-l">
<select name="newIndustry1" id="newIndustry1" lay-filter="newIndustry1">
<option value="">--请选择--</option>
<option value="">${project.newIndustry1 }</option> //这个是我的测试值,是有结果的
</select>
</div>
<div class="width180 float-l">
<select name="newIndustry2" id="newIndustry2" >
<option value="">--请选择--</option>
</select>
</div>
</td>
</tr>
var table = null;
var form = null;
var countyData = null;
var fn = {};
var newIndustryObj={
"newIndustry1":[
{"id":"newIndustry11","text":"下一代信息网络产业"},{"id":"newIndustry12","text":"信息技术服务"},{"id":"newIndustry13","text":"电子核心产业"},{"id":"newIndustry14","text":"网络信息安全产品和服务"},{"id":"newIndustry15","text":"人工智能"}
],
"newIndustry11":[
{"id":"newIndustry111","text":"网络设备"},{"id":"newIndustry112","text":"信息终端设备"},{"id":"newIndustry113","text":"网络运营服务"}
],
"newIndustry12":[
{"id":"newIndustry121","text":"新兴软件及服务"},{"id":"newIndustry122","text":"“互联网+”应用服务 "},{"id":"newIndustry123","text":"大数据服务 "}
],
"newIndustry13":[
{"id":"newIndustry131","text":"集成电路 "},{"id":"newIndustry132","text":"新型显示器件"},
{"id":"newIndustry133","text":"新型元器件"},{"id":"newIndustry134","text":"高端储能"},
{"id":"newIndustry135","text":"关键电子材料"},{"id":"newIndustry136","text":"电子专用设备仪器"},
{"id":"newIndustry137","text":"其他高端整机产品"}
]
};
//二级下拉框对应的input
<input type="hidden" id="newInd1" value="${project.newIndustry1 }"/>
<select name="newIndustry1" id="newIndustry1" lay-filter="newIndustry1">
<option value="">--请选择--</option>
<option value="">${project.newIndustry1 }</option> //这个是我的测试值,是有结果的
</select>
//三级下拉框对应的input框
<input type="hidden" id="newInd2" value="${project.newIndustry2 }"/>
<select name="newIndustry2" id="newIndustry2" style="width:217px; ">
<option value="">--请选择--</option>
</select>
然后在js里面
代码如下:
/**
* 定义初始化组件方法
*/
fn.init = function(){
//初始化数据表格
layui.use(['table', 'form'], function(){
table = layui.table;
form = layui.form;
fn.initComponent(); //初始化方法
});
};
/**
* 回显下拉框 Start 回显下拉框方法定义在fn.initComponent()中
*/
fn.initShowSelect = function(){
var newIndustry = $("select[name='newIndustry']").val(); //一级下拉框
var newInd1 = $("#newInd1").val(); //二级下拉框对应值存input框中
var newInd2 = $("#newInd2").val(); //三级下拉框对应值存在input中
//判断以一级下拉框是否存在值
if(newIndustry!=null&&newIndustry!=""){
var xx=newIndustryObj[newIndustry];
for(var j in xx){
//添加下拉框元素
$('#newIndustry1').append(new Option(xx[j].text, xx[j].id));
//如果下拉框中的元素有和对应值一样的的数据,则将对应值回显到下拉框
if(xx[j].id==newInd1){
$("select[name='newIndustry1']").val(xx[j].id);
}
}
form.render('select');
}
var newIndustry1 = $("select[name='newIndustry1']").val(); //二级级下拉框
//判断以二级下拉框是否存在值
if(newIndustry1!=null&&newIndustry1!=""){
var xx=newIndustryObj[newIndustry1];
for(var j in xx){
//添加下拉框元素
$('#newIndustry2').append(new Option(xx[j].text, xx[j].id));
//如果下拉框中的元素有和对应值一样的的数据,则将对应值回显到下拉框
if(xx[j].id==newInd2){
$("select[name='newIndustry2']").val(xx[j].id);
}
}
form.render('select');
}
}
/**
* 回显下拉框 end
*/
当然这种回显方法会有一个问题,就是会造成下拉框的option重叠,因为我这边是append的option,所以要用下面这个方法清空一下
当然代码的位置是放在下拉框联动的位置。我是通过form.on("select(newIndustry)"这个来控制代码联动,所以具体还是看自己的代码的写法。
代码示例
$("#newIndustry1").empty(); //清空一次下拉框
form.on("select(newIndustry)", function(selData){
var newIndustryVal = selData.value;
var xx=newIndustryObj[newIndustryVal];
$("#newIndustry1").empty(); //清空一次下拉框
for(var j in xx){
$('#newIndustry1').append(new Option(xx[j].text, xx[j].id));
}
form.render('select');
});