Layui中的監聽
阿新 • • 發佈:2021-01-17
目錄
監聽單選框
預期效果:監聽一個單選框,當點選通過時,input顯示“通過”;點選不通過時,顯示“不通過”。
程式碼如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>監聽單選框</title>
<link rel="stylesheet" href="../static/css/layui.css" >
</head>
<body>
<form class="layui-form" action="" method="post" onsubmit="return false" lay-filter="sample_check_form">
<div class="layui-form-item">
<label class="layui-form-label"> 稽核結果</label>
<div class="layui-input-block">
<input type="radio" lay-filter="result" name="result" value="1" title="<b><span style='color:green;'>通過</span></b>" >
<input type= "radio" lay-filter="result" name="result" value="-1" title="<b><span style='color:red;'>不通過</span></b>">
</div>
</div>
現在的操作結果:<input type="text" id="result_value" />
</form>
<script type="text/javascript" src="../static/layui.js"></script>
<script type="text/javascript" >
layui.use(['form'],function (){
var form = layui.form;
// 監聽 單選框
form.on('radio(result)', function (data) {
if(data.value == '1'){
document.getElementById("result_value").value = "通過";
}else{
document.getElementById("result_value").value = "不通過";
}
});
});
</script>
</body>
</html>
監聽開關
預期效果:監聽一個開關,當點選同意時,input顯示“我同意”;點選拒絕時,顯示“我拒絕”。
程式碼如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>監聽開關</title>
<link rel="stylesheet" href="../static/css/layui.css">
</head>
<body>
<form class="layui-form" action="" method="post" onsubmit="return false" lay-filter="sample_check_form">
<div class="layui-form-item" pane="">
<label class="layui-form-label">入贅我王家</label>
<div class="layui-input-block">
<input type="checkbox" checked="" name="res" lay-skin="switch" lay-filter="filter_switch" title="入贅我王家" lay-text="同意|拒絕">
</div>
</div>
操作結果:<input type="text" id="result_value" />
</form>
<script type="text/javascript" src="../static/layui.js"></script>
<script type="text/javascript" >
layui.use(['form'],function (){
var form = layui.form;
//監聽 指定開關
form.on('switch(filter_switch)', function(data){
if(this.checked){
document.getElementById("result_value").value = "我同意";
}else{
document.getElementById("result_value").value = "我拒絕";
}
});
});
</script>
</body>
</html>