jQuery的 - 从回调函数(后收费)的返回值到函数,其里面?函数、回调、返回值、里面

2023-09-10 13:51:59 作者:祖傳老中醫,專治吹牛逼

我有一个JavaScript函数,职位数据验证脚本,并抓住从那里值。在POST请求的回调函数返回一个布尔值,和我想要得到的全部的函数返回的布尔值。眼下,回调函数返回正确的值,但是函数本身不会返回任何东西。这里的code:

I have a javascript function that posts data to a validation script and grabs a value from there. The callback function on the post request returns a boolean value, and I'm trying to get the entire function to return that boolean value. Right now, the callback function returns the correct value, but the function itself doesn't return anything. Here's the code:

function validate(request_type, request_text) {
    $.post("http://www.example.com/ajax/validate.php",{
        type: request_type, 
        text: request_text
    }, function(data) {
        return (data == "valid");
    });
}

我知道这是有点一个同步称呼,这不是什么AJAX是什么,但我已经在validate.php众多功能(数据库调用等),我不能在Javascript执行,我看到像this 之一是谈论使用某种形式的处理程序。

I realise that this is sort of a "synchronous" call, and that's not what AJAX is about, but I already have numerous functions in validate.php (database calls, etc.) that I can't implement in Javascript, and I saw threads like this one that talk about using some form of handler.

我如何写一个简单的处理程序,这将使任何变量数据或布尔比较数据==的结果有效可当我在使用它的的if / else 语句(这是这个功能应该是使用)?

How would I write a simple handler that will make either the variable data or the result of the boolean comparison data == "valid" available when I use it in an if/else statement (which is where this function is supposed to be used)?

编辑:例如,将使用布尔结果如果语句之一:

For example, one of the if statements that will be using the boolean result:

if (!validate('password',pass_new)) {
        $('#pass_new').addClass('error');
        $('#pass_confirm_new').addClass('error');
        $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
        $('#pass_text_short').hide();
        $('#pass_text_long').show();

编辑:

function valid_pass_sett() {
    //code to remove errors left over from previous submissions - snipped
    pass_old = $('input[name=pass_old]').val();
    pass_new = $('input[name=pass_new]').val();
    pass_confirm_new = $('input[name=pass_confirm_new]').val();
    //some if statements that don't involve AJAX requests - snipped
    if (!validate('password',pass_new)) {
        $('#pass_new').addClass('error');
        $('#pass_confirm_new').addClass('error');
        $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
        $('#pass_text_short').hide();
        $('#pass_text_long').show();
        return false;
    }

    return true;
}

我还没有编辑这个code到包括更新code这是被贴了,但我的问题是我如何返回false 从它停止表单提交?

I haven't edited this code to include the updated code that's been posted, but my question is how I return false from it to stop form submission?

推荐答案

除非你做出的同步的AJAX调用(你可能不希望这样做),你根本就没有。

Unless you make a synchronous AJAX call (which you probably don't want to do), you simply can't.

如果使用此功能在好几个地方在code,你最好的选择可能是允许它的接收的功能。

If this function is used in several places in your code, your best bet may be to allow it to receive a function.

这样的话,而不是依靠其结果是从的你的功能,在某些code用于归还,你实际上是通过你的code直接的,所以它是保证能够使用响应

That way instead of relying on the result being returned from your function to be used in some code, you're actually passing your code directly in, so it is ensured to be able to use the response.

var my_form = $('#my_form');

my_form.submit( valid_pass_sett );

function valid_pass_sett() {
    //code to remove errors left over from previous submissions - snipped
    pass_old = $('input[name=pass_old]').val();
    pass_new = $('input[name=pass_new]').val();
    pass_confirm_new = $('input[name=pass_confirm_new]').val();

    validate('password', pass_new, pswd_validation_callback); // async validation

    return false;  // cancel form submission
}

function validate(request_type, request_text, callback ) {
    $.post("http://www.example.com/ajax/validate.php",{
        type: request_type, 
        text: request_text
    }, callback );
}

function pswd_validation_callback( data ) {
    if ( data === 'valid' ) {
         // if valid, call the native .submit() instead of the jQuery one
        my_form[ 0 ].submit();
    } else {
         // Otherwise do your thing for invalid passwords.
         // The form has already been canceled, so no concerns there.
        $('#pass_new').addClass('error');
        $('#pass_confirm_new').addClass('error');
        $(error_string.format('Please enter a valid password.')).insertAfter('#pass_confirm_new');
        $('#pass_text_short').hide();
        $('#pass_text_long').show();
    }
}

编辑: 更改为使用code发布有关的

编辑: 更新与额外code工作张贴。缩小答案到指定的功能清晰。的