javascript子函数传递返回到父函数返回 [英] javascript child function pass the return to the parent function return

查看:167
本文介绍了javascript子函数传递返回到父函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递的返回从一个孩子(我不知道正确的术语)函数到父函数的返回...如何正确的方式做到这一点? check_data()不返回true或false

i want to pass the return from a "child" (i don't know the correct term) function to the return in the parent function... how is the correct way to do this? check_data() isn't returning true or false

function check_data(type,field){
    var chkvalue = $(field).val();
    $.post("mods/ajax.fieldchk.php", {
        chkvalue: chkvalue,
        type: type
    },
    function(result){
        if(result==0){
            $(field).css({'background-color': '#faa', 'border': '1px solid #f00'});
            return false;
        }else if(result==1){
            $(field).css({'background-color': '#afa', 'border': '1px solid #0f0'});
            return true;
        }
    };
}


b $ b

thnx

thnx

推荐答案

由于 $。post 一个异步函数你不能使用它来返回check_data上的数据,但你可以传递一些回调函数并用 true / false 参数执行。

Since $.post is an asynchronous function you can't use it to return data on check_data. However you can pass some callback and execute with true/false argument.

这样的东西:

function check_data(type,field,callback){
    var chkvalue = $(field).val();
    $.post("mods/ajax.fieldchk.php", {
        chkvalue: chkvalue,
        type: type
    },
    function(result){
        if(result==0){
            $(field).css({'background-color': '#faa', 'border': '1px solid #f00'});
            callback(true);
        }else if(result==1){
            $(field).css({'background-color': '#afa', 'border': '1px solid #0f0'});
            callback(false);
        }
    };
}

这篇关于javascript子函数传递返回到父函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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