function checkName() { var deferred = $.Deferred();//延迟方法 var Name = $("#name").val(); var Names = $("#names").val(); var Pid = $("#pNodeId").val(); var nodeId = $("#nodeId").val(); $.ajax({ type:"post", url:"../Category/verifyName", async:false, data:{ Name:$("#name").val(), Pid:$("#pNodeId").val(), nodeId: $("#nodeId").val() }, dataType: "html", success: function(data, type) { if(data=="ok"){ deferred.resolve(); }else{ deferred.reject(); } } }); //deferred.state()有3个状态:pending:还未结束,rejected:失败,resolved:成功 return deferred.state() == "resolved" ? true : false; }
remote通过addMethod方式实现:
jQuery.validator.addMethod("valCheckName", function(value, element) { return checkName(); },‘名称已存在‘);
jquey.validate 中调用
//表单校验 $(‘#form‘).validate({ submitHandler:function(form){ if(checkName()){//提交前再次验证是否重名 form.submit(); } }, rules : { name : { required : true, byteRangeLength : [2, 32], valCheckName:true }, note : {byteRangeLength : [3, 200]} } });
时间: 2024-10-29 19:05:33