jQuery验证-有效后将不会触发远程方法 [英] jquery validation - remote method won't trigger after valid

查看:105
本文介绍了jQuery验证-有效后将不会触发远程方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

email1: {
                    required: true,
                    blacklist: true,
                    beanEmailValidator: true,
                    minlength: 6,
                    maxlength: 70,
                    remote: {
                        type: "GET",
                        url: llbFieldValJSONURL,
                        data: {
                              fieldData : function() {
                                return $("#enterEmail").val();
                              }                           
                         },
                        dataType:"json",
                        dataFilter: function(data) {
                            var json = jQuery.parseJSON(data);
                            if(json.error == "true") {
                                return "\"" + json.errorMessage + "\"";
                            } else {
                                return success;

                            }

                        }
                    }

                }

此函数检查用户名的唯一性,如果不唯一,则返回错误.问题是远程方法有效后将不会再进行任何其他调用.我需要每次输入/更改值时都触发它.

This function check the uniqueness of a username and returns an error if not unique. The issue is that the remote method won't make any additional calls after being valid.. I need it to trigger every time a value is entered/changed.

例如,如果我输入3个非唯一的用户名,则每次都会进行呼叫.如果输入唯一的用户名,则可以正确进行呼叫,并且该用户名将恢复为有效.因此该字段有效.现在,如果我输入另一个非唯一(无效)用户名,则远程方法将不会再次触发.

For example, if I enter 3 non-unique usernames, the call is made every time. If I enter a unique username, the call is made correctly and the username comes back as valid. So the field is then valid. Now if I enter another non-unique (invalid) username, the remote method won't trigger again.

想知道是否以某种方式缓存响应吗?

Wondering if it's somehow caching the response?

无效(非唯一)响应:

{"error":"true","errorMessage":"<b>The User Name you chose is already in use.</b>  Please enter another name."}

有效(唯一)响应:

{"error":"false","errorMessage":""}

修改

在SO上看到了这一点

https://stackoverflow.com/a/1821667/335514

$("#site").change(function() {
  $("#email").removeData("previousValue");
});

因此,该插件似乎正在缓存响应.插件是否具有关闭缓存的方法,或者将cache: false添加到ajax调用中会做同样的事情?

So the plugin seems to be caching the response. Does the plugin have a method to switch off caching, or would adding a cache: false to the ajax call do the same thing?

修改

这两种方法均无效.似乎一旦将字段标记为有效,它就不会再次进行远程呼叫.关于如何解决的想法?将函数放入自己的addMethod中可以防止这种情况吗?

Neither of these methods worked. It appears that once a field is marked valid, it won't make the remote call again. Thoughts on how to fix? Would putting the function into it's own addMethod prevent this scenario?

推荐答案

更新/解决方案:

查看jquery.validate.js的源代码,尤其是以下代码段:

Looking at the source code of jquery.validate.js, particularly this snippet:

success: function(response) {
    validator.settings.messages[element.name].remote = previous.originalMessage;
    var valid = response === true;

    if ( valid ) {   ...

由于响应被评估为JSON,因此您应该调用return "true". 文档

Since the response is evaluated as JSON, you are expected to call return "true". This is also pointed out in the documentation

响应被评估为JSON,对于有效元素必须为true"

"The response is evaluated as JSON and must be true for valid elements"

但是,如果忽略了以下内容是隐式设置的事实,那么在查看 完全等于 测试为true的源代码时,可能会造成混乱. dataType: json

However, it can be quite confusing when looking at the source code that does an exactly equals test for true, if you neglect the fact that the following is implicitly set dataType: json

结束更新

在进行更多调查后,我会说您的"dataFilter"不应返回成功"

Upon investigating some more, I'd say that your 'dataFilter' should not be returning 'success'

请参阅回调函数队列@ http://api.jquery.com/jQuery.ajax/

Refer to Callback function queues @ http://api.jquery.com/jQuery.ajax/

成功收到以下命令后,将立即调用

dataFilter回调 响应数据.它接收返回的数据和 dataType,并必须返回(可能已更改)数据以传递给 成功.

dataFilter callback is invoked immediately upon successful receipt of response data. It receives the returned data and the value of dataType, and must return the (possibly altered) data to pass on to success.

我的猜测是您的JS尝试执行return success时失败了,因此没有进一步的验证请求被发送

My guess is that your JS breaks on trying to do return success and thus no further validation requests are sent

这篇关于jQuery验证-有效后将不会触发远程方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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