Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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.each与$post一起将值发送到外部var不起作用_Jquery_Post_Each - Fatal编程技术网

jQuery.each与$post一起将值发送到外部var不起作用

jQuery.each与$post一起将值发送到外部var不起作用,jquery,post,each,Jquery,Post,Each,我正在尝试使用.each为每次循环到abilitiesExtra时添加数据,但目前它似乎不起作用,我不知道为什么 我认为这是出了什么问题: var abilitiesExtra = (0 * 1); $(".iconDiv").each(function () { if ($(this).is(":empty") === false) { //alert("Hello"); var abi

我正在尝试使用.each为每次循环到abilitiesExtra时添加数据,但目前它似乎不起作用,我不知道为什么

我认为这是出了什么问题:

  var abilitiesExtra = (0 * 1);
        $(".iconDiv").each(function () {
            if ($(this).is(":empty") === false) {
                //alert("Hello");
                var ability = $(this).children("img").first().attr("class");
                $.post('PHP/getAbilityCost.php', {
                    ability: ability
                }, function (dataTwo) {
                    abilitiesExtra = (dataTwo * 1) + abilitiesExtra;
                });
            }
        });
       alert(abilitiesExtra);
周围的代码:

 if (oneIsEmpty === "false") {
        var abilitiesCost = (0 * 1);
        var attack = $("#attack").val();
        var speed = $("#speed").val();
        var minRng = $("#minRng").val();
        var maxRng = $("#maxRng").val();
        var defense = $("#defense").val();
        var hitPoints = $("#hitPoints").val();
        var size = $("#size").val();
        $.post('PHP/getNoraCost.php', {
            attack: attack
            , speed: speed
            , minRng: minRng
            , maxRng: maxRng
            , defense: defense
            , hitPoints: hitPoints
            , size: size
        }, function (data) {
            abilitiesCost = (data * 1) + abilitiesCost;
        });
        var abilitiesExtra = (0 * 1);
        $(".iconDiv").each(function () {
            if ($(this).is(":empty") === false) {
                var ability = $(this).children("img").first().attr("class");
                $.post('PHP/getAbilityCost.php', {
                    ability: ability
                }, function (dataTwo) {
                    abilitiesExtra = (dataTwo * 1) + abilitiesExtra;
                    console.log(abilitiesExtra); //gives the right number
                });
            }
        });
        //alert(abilitiesExtra) is always 0 now
        abilitiesCost = abilitiesExtra + abilitiesCost;
        $("#totalNoraCostD p").empty();
        $("#totalNoraCostD p").append(abilitiesCost);
    }

ajax调用是异步的。调用
$.post()
时,它只发送ajax请求。调用
$.post()
之后的行将立即执行。稍后,当ajax调用返回时,将执行回调函数

函数返回一个对象,它是一个对象。您可以将所有
jqXHR
对象放在一个数组中,然后在最后一个ajax调用返回时使用执行回调

if (!oneIsEmpty) {
    var abilitiesCost = 0;
    var abilitiesExtra = 0;

    var costJqXhr = $.post('PHP/getNoraCost.php', {
        attack: $("#attack").val(),
        speed: $("#speed").val(),
        minRng: $("#minRng").val(),
        maxRng: $("#maxRng").val(),
        defense: $("#defense").val(),
        hitPoints: $("#hitPoints").val(),
        size: $("#size").val()
    }, function(data) {
        abilitiesCost += (data * 1);
    });

    // This makes the ajax calls, creating an array of the jqXhr objects.
    var jqXhrs = $(".iconDiv:not(:empty)").map(function() {
        return $.post('PHP/getAbilityCost.php', {
            ability: $(this).children("img:first").attr("class")
        }, function(dataTwo) {
            abilitiesExtra += (dataTwo * 1);
        });
    });

    jqXhrs.push(costJqXhr);

    // This executes the callback when the last ajax call has returned.
    $.when.apply($, jqXhrs).then(function() {
        $("#totalNoraCostD p").html(abilitiesCost + abilitiesExtra);
    });
}

abilitiesExtra=($.parseInt(dataTwo)*1)+$.parseInt(abilitiesExtra)检查itI在执行此操作时遇到以下错误:未捕获的TypeError:$.parseInt不是Object.success(index.js:179)n(jquery.min.js:2)Object.fireWith[as resolveWith](jquery.min.js:2)w(jquery.min.js:4)XMLHttpRequest.d(jquery.min.js:4)console.log(dataTwo)的函数;检查这个,我认为它不是一个数字或字符串。它是作为一个对象出现的控制台日志只是输出了正确的数字。让我添加周围的代码以及!输出是什么?你能通过屏幕截图显示吗?虽然代码现在运行时没有错误,但对于abilitiesCost和abilitiesExtra,它只返回0。所以还是一样的问题。谢谢你的帮助@Tibout-代码中有两个错误导致数组始终为空。(首先,我没有将
return
放在
$.map()
的回调中,第二,我在
.add()
应该是
.push()
的时候添加了
.add()
)我还添加了一个JSFIDLE,显示它可以工作。嘿,它现在几乎可以工作了!我唯一的问题是,如果所有的IConDiv都是空的,那么它就不工作了(它仍然应该工作),我试着处理这一切,但没有得到它。@Tibout-您是否将
.add()
更改为
.push()
?我在之前的评论中提到了它,在JSFIDLE中它是正确的,但我想我在回答中没有更新代码。就是这样,我忽略了它,因为我注意到您更改了返回!非常感谢!