layui之select的option疊加問題的解決方法
更新時間:2018年03月08日 15:14:43 作者:小飛博客
下面小編就為大家分享一篇layui之select的option疊加問題的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
小編我在使用layui,在select的地方遇到了坑,select里的值居然無法清空,select里的option還有疊加問題,為了解決這個問題,也達到我的功能,我研究了下,讓有同樣需求的小伙伴不踩坑,特貼上我的源碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>layui-下拉框聯(lián)動測試</title>
<link rel="stylesheet" href="layui/css/layui.css" rel="external nofollow" >
</head>
<body>
<div id="wrap">
<div class="layui-form" lay-filter="merchantForm">
<div class="layui-form-item">
<label class="layui-form-label">選擇框</label>
<div class="layui-input-block">
<select name="city" lay-verify="required" id="test1" lay-filter="test1">
<option value="0">時間</option>
<option value="1">金額</option>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">選擇框</label>
<div class="layui-input-block">
<select name="city" lay-verify="required" id="test2" lay-filter="test2">
<option value="">請選擇規(guī)則名稱</option>
</select>
</div>
</div>
</div>
<button id="btn">確定</button>
</body>
<script src="layui/layui.all.js"></script>
<script src="layui/jquery-1.8.3.min.js"></script>
<script>
//后臺傳過來的數(shù)據(jù)
var data=[
{unitType:0,ruleName:'時間規(guī)則11',amount:1100,money:1100},
{unitType:0,ruleName:'時間規(guī)則12',amount:1200,money:1200},
{unitType:0,ruleName:'時間規(guī)則13',amount:1300,money:1300},
{unitType:1,ruleName:'金額規(guī)則21',amount:2100,money:2100},
{unitType:1,ruleName:'金額規(guī)則22',amount:2200,money:2200},
{unitType:1,ruleName:'金額規(guī)則23',amount:2300,money:2300},
];
//初始化默認為時間類型以及對應(yīng)的時間規(guī)則
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');
})
//動態(tài)二級聯(lián)動
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');
});
})
//二級聯(lián)動完畢后獲取對應(yīng)的規(guī)則數(shù)據(jù)
$('#btn').click(function(){
var thisValue=data.find((v)=>v.ruleName==$('#test2').val());
console.log(thisValue);
})
</script>
</html>
以上這篇layui之select的option疊加問題的解決方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
js/jquery控制頁面動態(tài)加載數(shù)據(jù) 滑動滾動條自動加載事件的方法
下面小編就為大家?guī)硪黄猨s/jquery控制頁面動態(tài)加載數(shù)據(jù) 滑動滾動條自動加載事件的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

