您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

layui中select的option叠加问题

2025/10/6 10:27:17发布12次查看
在使用layui,在select的地方遇到了坑,select里的值居然无法清空,select里的option还有叠加问题,为了解决这个问题,也达到我的功能,我研究了下,让有同样需求的小伙伴不踩坑,特贴上我的源码:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>layui-下拉框联动测试</title> <link rel="stylesheet" href="layui/css/layui.css" rel="external nofollow" > </head> <body> <p id="wrap"> <p class="layui-form" lay-filter="merchantform"> <p class="layui-form-item"> <label class="layui-form-label">选择框</label> <p class="layui-input-block"> <select name="city" lay-verify="required" id="test1" lay-filter="test1"> <option value="0">时间</option> <option value="1">金额</option> </select> </p> </p> <p class="layui-form-item"> <label class="layui-form-label">选择框</label> <p class="layui-input-block"> <select name="city" lay-verify="required" id="test2" lay-filter="test2"> <option value="">请选择规则名称</option> </select> </p> </p> </p> <button id="btn">确定</button> </body> <script src="layui/layui.all.js"></script> <script src="layui/jquery-1.8.3.min.js"></script> <script> //后台传过来的数据 var data=[ {unittype:0,rulename:'时间规则11',amount:1100,money:1100}, {unittype:0,rulename:'时间规则12',amount:1200,money:1200}, {unittype:0,rulename:'时间规则13',amount:1300,money:1300}, {unittype:1,rulename:'金额规则21',amount:2100,money:2100}, {unittype:1,rulename:'金额规则22',amount:2200,money:2200}, {unittype:1,rulename:'金额规则23',amount:2300,money:2300}, ]; //初始化默认为时间类型以及对应的时间规则 layui.use('form', function(){ var form = layui.form; $('#test2').html(''); var html=''; $.each(data,function(i,e){ if(e.unittype==0) html+=`<option data-type="${e.unittype}">${e.rulename}</option>`; }) $('#test2').append(html); form.render('select'); }) //动态二级联动 layui.use('form', function(){ var form = layui.form; form.on('select(test1)', function(obj){ $('#test2').html(''); var html=''; if(obj.value==0){ $.each(data,function(i,e){ if(e.unittype==obj.value) html+=`<option data-type="${e.unittype}">${e.rulename}</option>`; }) $('#test2').append(html); }else if(obj.value==1){ $.each(data,function(i,e){ if(e.unittype==obj.value) html+=`<option data-type="${e.unittype}">${e.rulename}</option>`; }) $('#test2').append(html); } form.render('select'); }); }) //二级联动完毕后获取对应的规则数据 $('#btn').click(function(){ var thisvalue=data.find((v)=>v.rulename==$('#test2').val()); console.log(thisvalue); }) </script> </html>
相关推荐:
有关边界叠加的课程推荐
使用css中margin边界叠加问题及解决方案
使用嵌套select子式 解决mysql不能叠加使用如max(sum())的问题
以上就是layui中select的option叠加问题的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product