Home  >  Article  >  Web Front-end  >  Layui event listening methods

Layui event listening methods

尚
forward
2019-11-30 12:03:154359browse

Layui event listening methods

When using layui's form form for verification and submission, if it is combined with vue or three-level linkage, event monitoring is required.

Specific syntax:

form.on('event(filter value)', callback);

Can be used to monitor: select, checkbox, switch , radio, submit changes

1. Monitoring select changes

<!-- 不用form 用div也可以 -->
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">下拉选择框</label>
        <div class="layui-input-block">
            <select name="interest" lay-filter="aihao">
                <option value="0">写作</option>
                <option value="1">阅读</option>
                <option value="2">听歌</option>
                <option value="4">游戏</option>
            </select>
        </div>
    </div>
</form>
<script type="text/javascript" src="./layui/layui.js"></script>
<script type="text/javascript">
layui.use(&#39;form&#39;, function(){
    var form = layui.form;
    form.on(&#39;select(aihao)&#39;,function(data){
        console.log(data);
        console.log(data.elem); //得到select原始DOM对象
        console.log(data.value); //得到被选中的值
        console.log(data.othis); //得到美化后的DOM对象
    });
});
</script>

Similarly, monitoring checkbox check

form.on(&#39;checkbox(filter)&#39;, function(data){
    console.log(data.elem); //得到checkbox原始DOM对象
    console.log(data.elem.checked); //是否被选中,true或者false
    console.log(data.value); //复选框value值,也可以通过data.elem.value得到
    console.log(data.othis); //得到美化后的DOM对象
});

Monitoring switch switch:

form.on(&#39;switch(filter)&#39;, function(data){
  console.log(data.elem); //得到checkbox原始DOM对象
  console.log(data.elem.checked); //开关是否开启,true或者false
  console.log(data.value); //开关value值,也可以通过data.elem.value得到
  console.log(data.othis); //得到美化后的DOM对象
});

Monitor radio single selection:

form.on(&#39;radio(filter)&#39;, function(data){
    console.log(data.elem); //得到radio原始DOM对象
    console.log(data.value); //被点击的radio的value值
});

Monitor submit submission:

<button lay-submit lay-filter="*">提交</button>
form.on(&#39;submit(*)&#39;, function(data){
    console.log(data.elem) //被执行事件的元素DOM对象,一般为button对象
    console.log(data.form) //被执行提交的form对象,一般在存在form标签时才会返回
    console.log(data.field) //当前容器的全部表单字段,名值对形式:{name: value}
    return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});

For more layui knowledge, please pay attention to the layui usage tutorial column.

The above is the detailed content of Layui event listening methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete