当前位置 : 主页 > 网络编程 > JavaScript >

validation+layer+ajax 提交验证表单

来源:互联网 收集:自由互联 发布时间:2021-06-30
validation+layer+ajax 提交验证表单 $("#form-user-add").validate({rules:{admin_name:{required:true,maxlength:16,},admin_cname:{required:true,maxlength:8,},email:{required: true, email: true}}, messages: { admin_name: { required: "请输入
validation+layer+ajax 提交验证表单
$("#form-user-add").validate({
		rules:{
			admin_name:{
				required:true,
				maxlength:16,
			},
			admin_cname:{
				required:true,
				maxlength:8,
			},
			email:{
				required: true,
                email: true
			}
		},
        messages: {
            admin_name: {
            	required: "请输入用户账号",
            	maxlength: "账号长度不能大于 16 个字符!"
        	},
            admin_cname: {
                required: "请输入用户姓名",
                maxlength: "用户名不能大于8个字符!"
            },
            email: "请输入一个正确的邮箱"
        },
		onkeyup:false,
		focusCleanup:true,
		/* 失去焦点时不验证 */
		onfocusout: false,
		success:"valid",
		//重写showErrors
        showErrors: function (errorMap, errorList) {
        	var msg = "";
        	$.each(errorList, function (i, v) {
        		//msg += (v.message + "\r\n");
        		//在此处用了layer的方法,显示效果更美观
        		layer.tips(v.message, v.element, { time: 2000 });
        		return false;
        	});  
        },
		submitHandler:function(form){
			$(form).ajaxSubmit({
				type: 'post',
				url: "{:url('Aduser/aduserAdd')}" ,
				success: function(json){
					layer.msg(json.msg,{icon:json.status,time:1000});
					//只有成功时,关闭弹框刷新父级iframe页面
					if (json.status===1) {
						setTimeout(function() { 
                			window.parent.location.reload(); 
            			}, 1000);
					}
					
				},
                error: function(XmlHttpRequest, textStatus, errorThrown){
					layer.msg('error!',{icon:2,time:1000});
				}
			});
			//自动关闭弹窗
			// var index = parent.layer.getFrameIndex(window.name);
			// parent.$('.btn-refresh').click();
			// parent.layer.close(index);
		}
	});
上一篇:ajaxSuccess
下一篇:jquery插件模板
网友评论