var indexAdd = null; $('#btnAddCate').on('click', function () { indexAdd = layer.open({ type: 1, area: ['500px', '250px'], title: '添加文章分类', content: $('#dialog-add').html() }); }) //通过代理的方式为form-add表单绑定submit(因为若是绑定到form-add身上 // 它有可能还不存在,所以要使用代理) $('body').on('submit', '#form-add', function (e) { e.preventDefault(); $.ajax({ method: 'POST', url: '/my/article/addcates', data: $(this).serialize(), success: function (res) { if (res.status !== 0) { return layer.msg('新增分类失败') } initArtCateList(); layer.msg('新增分类成功') layer.close(indexAdd);//根据索引关闭弹出层 } }) })
<script type="text/html" id="dialog-add"> <form class="layui-form" id="form-add"> <div class="layui-form-item"> <label class="layui-form-label">分类名称</label> <div class="layui-input-block"> <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">分类别名</label> <div class="layui-input-block"> <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button> <button type="reset" class="layui-btn layui-btn-primary">重置</button> </div> </div> </form> </script>
标签:function,layer,indexAdd,form,layui,分类,出层,add,小结 From: https://www.cnblogs.com/alwaysrun/p/16609861.html