jQuery验证异步不起作用 [英] Jquery validation async not working

查看:82
本文介绍了jQuery验证异步不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了此功能,该功能似乎正常工作,但它会返回#",提示要为AJAX加密.我试图删除回叫,没有外观.

I have made this function, that seems to work fine, but it return "#" insted of wainting for the AJAX. I have tried to remove the return call, with out look.

它需要等到AJAX完成后才能返回. JSP运行正常.

It need to wait until the AJAX is finish before return. The JSP is working fine.

怎么了?

jQuery.validator.addMethod("knidExist", function(value, element) {
    var valid = "#";
      $.ajax({
          async: false,
          type: "POST",
          url: "USER.jsp",
          data: "knid="+value,
          dataType: "html",
          success: function(msg) {
              // if the user exists, it returns a string "true"
              if($.trim(msg) != "true") {
                 //return false;
                 valid = false;  // already exists
              } else {
                 //return true;
                 valid = true;      // username is free to use
              }
          }  
     });
    return valid;
}, 'This USER does not exist');

推荐答案

我自己找到了一个方法.几乎是远程方法的完整副本,但带有静态消息和url. 希望我能帮上忙.

I found a method by my self. Almost a complete copy from the remote method but with static message and url. Hope i can help some one.

jQuery.validator.addMethod("userExist", function(value, element, param) {
if ( this.optional(element) )
    return "dependency-mismatch";

var previous = this.previousValue(element);
if (!this.settings.messages[element.name] )
    this.settings.messages[element.name] = {};
previous.originalMessage = this.settings.messages[element.name].remote;
this.settings.messages[element.name].remote = previous.message;

param = typeof param == "string" && {url:param} || param;

if ( this.pending[element.name] ) {
    return "pending";
}
if ( previous.old === value ) {
    return previous.valid;
}

previous.old = value;
var validator = this;
this.startRequest(element);
var data = {};
data["**user**"] = value;
$.ajax($.extend(true, {
    url: "validation.jsp",
    mode: "abort",
    dataType: "json",
    data: data,
    success: function(response) {
        validator.settings.messages[element.name].remote = "**This user do not exist"**;
        var valid = response === true;
        if ( valid ) {
            var submitted = validator.formSubmitted;
            validator.prepareElement(element);
            validator.formSubmitted = submitted;
            validator.successList.push(element);
            validator.showErrors();
        } else {
            var errors = {};
            var message = response || validator.defaultMessage( element, "remote" );
            errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
            validator.showErrors(errors);
        }
        previous.valid = valid;
        validator.stopRequest(element, valid);
    }
}, param));
return "pending";
}, "");

这篇关于jQuery验证异步不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆