<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">文章标题</label>
<div class="layui-input-block">
<input type="text" class="layui-input newsName" lay-verify="required" placeholder="请输入文章标题12312" name='title'>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">自定义属性</label>
<div class="layui-input-block">
<input type="checkbox" name="tuijian" class="tuijian" title="推荐">
<input type="checkbox" name="shenhe" class="newsStatus" title="审核">
<input type="checkbox" name="show" class="isShow" title="展示">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">文章作者</label>
<div class="layui-input-inline">
<input type="text" class="layui-input newsAuthor" lay-verify="required" placeholder="请输入文章作者">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">发布时间</label>
<div class="layui-input-inline">
<input type="text" class="layui-input newsTime" lay-verify="required|date" onclick="layui.laydate({elem:this})">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">浏览权限</label>
<div class="layui-input-inline">
<select name="browseLook" class="newsLook" lay-filter="browseLook">
<option value="0">开放浏览</option>
<option value="1">会员浏览</option>
</select>
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">关键字</label>
<div class="layui-input-block">
<input type="text" class="layui-input" placeholder="请输入文章关键字">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">内容摘要</label>
<div class="layui-input-block">
<textarea placeholder="请输入内容摘要" class="layui-textarea"></textarea>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章内容</label>
<div class="layui-input-block">
<textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="news_content"></textarea>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit="" lay-filter="addNews">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
form.on("submit(addNews)",function(data){
debugger
//是否添加过信息
if(window.sessionStorage.getItem("addNews")){
addNewsArray = JSON.parse(window.sessionStorage.getItem("addNews"));
}
//显示、审核状态
var isShow = data.field.show=="on" ? "checked" : "",
newsStatus = data.field.shenhe=="on" ? "审核通过" : "待审核";
addNews = '{"newsName":"'+$(".newsName").val()+'",'; //文章名称
addNews += '"newsId":"'+new Date().getTime()+'",'; //文章id
addNews += '"newsLook":"'+$(".newsLook option").eq($(".newsLook").val()).text()+'",'; //开放浏览
addNews += '"newsTime":"'+$(".newsTime").val()+'",'; //发布时间
addNews += '"newsAuthor":"'+$(".newsAuthor").val()+'",'; //文章作者
addNews += '"isShow":"'+ isShow +'",'; //是否展示
addNews += '"newsStatus":"'+ newsStatus +'"}'; //审核状态
addNewsArray.unshift(JSON.parse(addNews));
window.sessionStorage.setItem("addNews",JSON.stringify(addNewsArray));
//弹出loading
var index = top.layer.msg('数据提交中,请稍候',{icon: 16,time:false,shade:0.8});
setTimeout(function(){
top.layer.close(index);
top.layer.msg("文章添加成功!");
layer.closeAll("iframe");
//刷新父页面
parent.location.reload();
},2000);
return false;
})
关键点:
1. form表单一定要有class=‘layui-form’
2. 提交按钮的代码一定要这样写:
<button class="layui-btn" lay-submit="" lay-filter="addNews">立即提交<button>
一定要包含lay-filter.
3.表单的单个元素要这样写:
<input type="text" class="layui-input newsName" lay-verify="required" placeholder="请输入文章标题12312" name='title'>
一定要写name="xxx",
因为这样写,在Js代码中才能获取到对应的值,获取表单值,这样写。
form.on("submit(addNews)",function(data){
//对应的值这个获取
var title=data.filed.title;
var address=data.filed.address;
.......
var xxx=data.filed.xxx;
}