欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lauyi表单数据提交格式(ajax)

程序员文章站 2022-07-12 18:15:11
...

页面

  <form class="layui-form" action="" lay-filter="example" method="post" >

 <button class="layui-btn" lay-submit="" lay-filter="自定义名字" type="submit">立即提交</button>
 </form>

js代码

   layui.use(['form', 'layedit', 'laydate'], function () {
                var form = layui.form
                    , layer = layui.layer
                    , layedit = layui.layedit
                    , laydate = layui.laydate;

                laydate.render({
                    elem: '#date1'
                });

                //日期
                laydate.render({
                    elem: '#date1'
                });


                //监听提交
                form.on('submit(onSubmitAction)', function (data) {
				//验证
				       var moblie = data.field.mobile;
				        var reg=/^1[34578]\d{9}$/;
				        if(!reg.test(moblie)){
				          alert("手机号码格式不正确");
				          return false
				        }
				
				        var stuname = data.field.stuname;
				        if(stuname==null||stuname==""){
				          alert("姓名不能为空");
				          return false
				        }
				
				        var sportname = data.field.sportname;
				        if(sportname ==null||sportname ==""){
				          alert("请选择你想参加的运动项目");
				          return false
				        }
				
				        var faculty = data.field.faculty;
				        if(faculty ==null||faculty ==""){
				          alert("请选择你的院系");
				          return false
				        }


                    var insertData = data.field;
                    insertData.method="createSport";

                    $.ajax({
                        url: baseUrl + "/api/sport",
                        data: insertData,
                        type: "post",
                        dataType: "json",
                        success: function (jsonData) {
                            console.log(jsonData);
                            layer.msg('报名成功', {
                                icon: 1,
                                time: 1000 //1秒关闭(如果不配置,默认是3秒)
                            }, function () {

                            });
                        }
                    })
                    return false;
                })

            });