欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jquery.validationEngine 验证 AJAX 不起作用解决办法

程序员文章站 2022-03-09 10:53:30
...
使用ajax时 返回必须是json形式:
json名字可以随意,但[0]位置 必须是验证的控件id,[1]位置是是否成功





使用ajax时 一直处于等待验证状态,验证不起作用:





原因是因为内置json接收方式不对,所以 我们需要修改js文件代码



打开 jquery.validationEngine.js 文件

搜索 "$.ajax" 找到第二个 $.ajax

找到 success 回调处理函数

jsonValidateReturn就是你ajax处理返回的json数据的名字


success: function(json) {
                         // asynchronously called on success, data is the json answer from the server
                         var errorFieldId = json[0];   //修改这里  json[0] 修改成json.jsonValidateReturn[0]
                         //var errorField = $($("#" + errorFieldId)[0]);
                         var errorField = $($("input[id='" + errorFieldId +"']")[0]);

                         // make sure we found the element
                         if (errorField.length == 1) {
                             var status = json[1];     //修改这里  json[1] 修改成json.jsonValidateReturn[1]
                             // read the optional msg from the server
                             var msg = json[2];        //修改这里  json[2] 修改成json.jsonValidateReturn[2]  我们并没有返回消息 所以这里是null
                             if (!status) {
                                 // Houston we got a problem - display an red prompt
                                 options.ajaxValidCache[errorFieldId] = false;
                                 options.isError = true;

                                 // resolve the msg prompt
                                 if(msg) {
                                     if (options.allrules[msg]) {
                                         var txt = options.allrules[msg].alertText;
                                         if (txt) {
                                            msg = txt;
                     }
                                     }
                                 }
                                 else
                                    msg = rule.alertText;

                                 methods._showPrompt(errorField, msg, "", true, options);
                             } else {
                                 if (options.ajaxValidCache[errorFieldId] !== undefined)
                                    options.ajaxValidCache[errorFieldId] = true;

                                 // resolves the msg prompt
                                 if(msg) {
                                     if (options.allrules[msg]) {
                                         var txt = options.allrules[msg].alertTextOk;
                                         if (txt) {
                                            msg = txt;
                     }
                                     }
                                 }
                                 else
                                 msg = rule.alertTextOk;

                                 // see if we should display a green prompt
                                 if (msg)
                                    methods._showPrompt(errorField, msg, "pass", true, options);
                                 else
                                    methods._closePrompt(errorField);
                             }
                         }
                         errorField.trigger("jqv.field.result", [errorField, options.isError, msg]);
                     }