在Jquery validation里验证通过后,自定义提交不同的action路径,适合一个窗口既可以作为添加对话框也可以作为编辑对话框
$("#myform").validate({ debug: true, //调试模式取消submit的默认提交功能 ignore: '#Password,#ConfirmPassword', focusInvalid: false, //--------------------------------------------------------------------------- submitHandler: function () { //表单提交句柄,为一回调函数 var submitway = $("#submitbtn").text(); if (submitway == "编辑保存") var geturl = "/membership/update"; else if (submitway == "添加保存") var geturl = "/membership/add"; var options = { type: "post", url: geturl, dataType: "json", data: $("#myform").serialize(), success:function (data) { if (data.success) { alert(data.messages); $("#AddMembership").modal('hide'); window.parent.location.reload(); } else alert(data.messages); } }; $.ajax(options); return false; }, //--------------------------------------------------------------------------- rules: { 'Email': { required: true, email: true }, 'UserName': { required: true, stringCheck: true, minlength: 3, maxlength: 20 }, 'Mobile':{ //required:true, //带上不句表示此项不填写也可以,如果填写就要检查填写规则 isMobile:true }, 'Password': { required: true, minlength: 6, maxlength: 20 }, 'ConfirmPassword': { required: true, minlength: 6, equalTo: "#Password" }, 'RoleName': { required: true }, 'IsApproved': { required: true }, 'Gender': { required: true } }, messages: { UserName: { required: "请填写登录用户名", stringCheck: "用户名只能包括中文字、英文字母、数字和下划线", minlength: "用户名必须在3-20个字符之间(一个中文字算2个字符)" }, ///Email: "请输入您的邮箱。", Password:{required:"请输入您的密码",minlength:"密码至少要6个字符以上!"} } });
适用版本:jquery-1.8.3.min.js 、jquery-2.0.3.min.js
微信号:jamesworkshop 学习QQ群:364976091