Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery验证和在一个ajax调用中检索值_Jquery_Jquery Validate - Fatal编程技术网

jQuery验证和在一个ajax调用中检索值

jQuery验证和在一个ajax调用中检索值,jquery,jquery-validate,Jquery,Jquery Validate,在我们的web应用程序中,我们希望在单击某个按钮后立即使用ajax调用处理某个文本框的值: $("#the-button").click(function() { if ($("#the-form").validate().element($("the-button"))) { var oldValue = $("#the-button").value(), newValue = process(oldValue); } }); proc

在我们的web应用程序中,我们希望在单击某个按钮后立即使用ajax调用处理某个文本框的值:

$("#the-button").click(function() {
    if ($("#the-form").validate().element($("the-button"))) {
        var oldValue = $("#the-button").value(),
            newValue = process(oldValue);
    }
});
process()
方法隐藏了一个
synchronous
ajax调用,该调用基于文本框中的文本检索一些值

在执行process()之前,我们使用一个自定义适配器验证该字段,该适配器使用相同的ajax调用,只是为了查看服务器是否能够处理该值。(这是有原因的。)

现在,我们希望对此进行优化:当且仅当可以处理条目时,条目基本上是有效的:

因此,如果我们能够一次性验证和处理价值,那就太好了:

$.validator.addMethod("canBeProcessed", function(value, element) {
    var result;

    $.ajax({
        ...,
        async: false,
        success: function (data) {
            result = true;
            saveForLaterUse(data); // <-- save the result
        }
        error: function () {
            result = false;
        }
    }

    return result;
}, "Value cannot be processed");
$.validator.addMethod(“canBeProcessed”),函数(值,元素){
var结果;
$.ajax({
...,
async:false,
成功:功能(数据){
结果=真;
保存供以后使用(数据)//
$.validator.addMethod("canBeProcessed", function(value, element) {
    var result;

    $.ajax({
        ...,
        async: false,
        success: function (data) {
            result = true;
            saveForLaterUse(data); // <-- save the result
        }
        error: function () {
            result = false;
        }
    }

    return result;
}, "Value cannot be processed");