<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="../layui/css/layui.css">
<script src="../layui/layui.js"></script>
</head>
<body>
<form class="layui-form">
<!--提示:如果你不想用form,你可换成div等任何一个元素-->
<div class="layui-form-item">
<label class="layui-from-label">输入框</label>
<div class="layui-input-block">
<input type="text" name="input" layui-verify="required\number" placeholder="请输入"
autocomplete="off" class="layui-input" lay-filter="input" >
</div>
</div>
<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>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-input-block">复选框</label>
<div class="layui-input-block" >
<input type="checkbox" lay-filter="like" name="like[write]" title="写作" value="1" >
<input type="checkbox" lay-filter="like" name="like[read]" title="阅读" >
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">开关关</label>
<div class="layui-input-block">
<input type="checkbox" lay-skin="switch" lay-filter="switch" name="switch">
</div>
</div>
<div class="layui-form-item">
<label class="layui-from-label">单选框</label>
<div class="layui-input-block">
<input type="radio" name="sex" value="0" title="男" lay-filter="radio">
<input type="radio" name="sex" value="1" title="女" checked lay-filter="radio">
</div>
</div>
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">请填写描述</label>
<div class="layui-input-block">
<textarea placeholder="请输入内容" class="layui-textarea" name="textarea"></textarea>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block" >
<button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
<button type="rest" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
<script>
layui.use('form',function () {
var form=layui.form;
//获取select的值 根据 aihao来获取值
form.on('select(aihao)', function(data){
console.log(data.elem); //得到select原始DOM对象
console.log(data.value); //得到被选中的值
console.log(data.othis); //得到美化后的DOM对象
});
//获取 checkbox 的值 根据 lay-filter="like" checkbox(like)
form.on('checkbox(like)', 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对象
});
//获取 swith开关 的值 根据 lay-filter="switch" switch(switch)
form.on('switch(switch)', 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对象
});
//获取 radio 的值 根据 lay-filter="radio" radio(radio)
form.on('radio(radio)',function (data) {
console.log(data.elem); //得到radio 原始DOM对象
console.log(data.value); //被点击的radio的value的值
});
//获取 submit 的值 根据 lay-filter="*" submit(*)
form.on('submit(*)', function(data){
console.log(data.elem) //被执行事件的元素DOM对象,一般为button对象
console.log(data.form) //被执行提交的form对象,一般在存在form标签时才会返回
console.log(data.field) //当前容器的全部表单字段,名值对形式:{name: value}
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
});
</script>
</body>
</html>
submit 获取的值为 : 获取的都是那么的值
input: "1"
interes: "0"
like[read]: "on"
sex: "0"
switch: "on"
textarea: "111"