layui之select的option疊加問(wèn)題的解決方法
小編我在使用layui,在select的地方遇到了坑,select里的值居然無(wú)法清空,select里的option還有疊加問(wèn)題,為了解決這個(gè)問(wèn)題,也達(dá)到我的功能,我研究了下,讓有同樣需求的小伙伴不踩坑,特貼上我的源碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>layui-下拉框聯(lián)動(dòng)測(cè)試</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">時(shí)間</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="">請(qǐng)選擇規(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>
//后臺(tái)傳過(guò)來(lái)的數(shù)據(jù)
var data=[
{unitType:0,ruleName:'時(shí)間規(guī)則11',amount:1100,money:1100},
{unitType:0,ruleName:'時(shí)間規(guī)則12',amount:1200,money:1200},
{unitType:0,ruleName:'時(shí)間規(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},
];
//初始化默認(rèn)為時(shí)間類型以及對(duì)應(yīng)的時(shí)間規(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');
})
//動(dòng)態(tài)二級(jí)聯(lián)動(dòng)
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');
});
})
//二級(jí)聯(lián)動(dòng)完畢后獲取對(duì)應(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疊加問(wèn)題的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
幾種延遲加載JS代碼的方法加快網(wǎng)頁(yè)的訪問(wèn)速度
如何延遲javascript代碼的加載,加快網(wǎng)頁(yè)的訪問(wèn)速度,為了讓我們的網(wǎng)頁(yè)加載速度更快,本文總結(jié)了一下幾個(gè)注意點(diǎn),感興趣的朋友可以參考下2013-10-10
Cropper.js入門之在HTML中實(shí)現(xiàn)交互式圖像裁剪
這篇文章主要為大家介紹了Cropper.js入門之在HTML中實(shí)現(xiàn)交互式圖像裁剪示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
那些項(xiàng)目中常見的TypeScript錯(cuò)誤總結(jié)
這篇文章主要給大家總結(jié)介紹了一些項(xiàng)目中常見的TypeScript錯(cuò)誤的相關(guān)資料,如果你想查看所有的錯(cuò)誤信息和錯(cuò)誤碼,可以瀏覽TypeScript的源代碼倉(cāng)庫(kù),當(dāng)然隨著?ts?版本的更新,官網(wǎng)也會(huì)逐漸增加更多新的類型錯(cuò)誤,需要的朋友可以參考下2022-03-03
javascript中cookie對(duì)象用法實(shí)例分析
這篇文章主要介紹了javascript中cookie對(duì)象用法,實(shí)例分析了javascript針對(duì)cookie對(duì)象的寫入、讀取及刪除等操作的使用方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
使用javascript實(shí)現(xiàn)Iframe自適應(yīng)高度
這篇文章主要介紹了使用javascript實(shí)現(xiàn)Iframe自適應(yīng)高度,需要的朋友可以參考下2014-12-12
Javascript實(shí)現(xiàn)從小到大的數(shù)組轉(zhuǎn)換成二叉搜索樹
這篇文章主要介紹了Javascript實(shí)現(xiàn)從小到大的數(shù)組轉(zhuǎn)換成二叉搜索樹的相關(guān)資料,需要的朋友可以參考下2017-06-06
js學(xué)使用setTimeout實(shí)現(xiàn)輪循動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了js使用setTimeout實(shí)現(xiàn)輪循動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

