layui 表单提交

HTML 代码

<link rel="stylesheet" href="/tp5/public/static/layui/css/layui.css">

 <form class="layui-form" action="{:url('admin/adminRoleSave')}" method="post">
    <div class="layui-form-item">
         <label class="layui-form-label">角色名称</label>
          <div class="layui-input-block">
              <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
    </div>
    <div class="layui-form-item layui-form-text">
         <label class="layui-form-label">请填写描述</label>
         <div class="layui-input-block">
            <textarea name="remarks" placeholder="请输入内容" class="layui-textarea"></textarea>
     	</div>
    </div>
    <div class="layui-form-item">
         <div class="layui-input-block">
               <button class="layui-btn" lay-submit lay-filter="demo">新增角色</button>
         </div>
     </div>
</form>

javascript代码

script src="/tp5/public/static/js/jquery.min.js"></script>
<script src="/tp5/public/static/layui/layui.js"></script>
<script>
    layui.use('form', function(){
        var form = layui.form();
        //监听提交
        form.on('submit(*)', function(data){
            $.ajax({
                url:data.form.action,
                type:data.form.method,
                data: data.field,
                success: function (info) {
                   if (info.code === 1) {
                       setTimeout(function () {
                          location.href = info.url;
                       }, 1000);
                  }
                    layer.msg(info.msg);
                }
            });
            return false;
        });
    });
</script>