【问题标题】:如何在 jQuery 中的每个之后执行代码
【发布时间】:2022-01-23 06:50:38
【问题描述】:

我需要在 jQuery .each 之后执行 som 代码

我试图在每个循环之后写它,但它会立即执行。 我曾尝试使用 .promise().done,但这给了我一个错误,即 promise() 不是函数。

我的代码

$("#loader").show();
            var url = $(this).attr("action");
            var formData = new FormData($(this)[0]);
            $(this).hide(); 
            $.ajax({
                url: "generateJson.php",
                type: 'POST',
                data: formData,
                async: true,
                cache: false,
                contentType: false,
                enctype: 'multipart/form-data',
                processData: false,
                success: function (data, status)
                {
                    $('#json').append(data); //content loads here

                },
                error: function (xhr, desc, err)
                {
                    console.log("error");
                }
            }).done(function( data ) {
                data = JSON.parse( $("#json").val() );
                //console.log(data.length);
                $.each( data, function( key, val ) {
                    console.log(val);
                    //$("#result").append(val);
                    $.ajax({
                        type: "POST",
                        url: "/generateInvoice.php",
                        dataType: "html",
                        data: val,
                        success: function (data, status) {
                            //console.log(data);
                            if (data) {
                                $("#result").append(data)
                            //  console.log("Hurray");
                            } else {
                                console.log("Error");
                            }
                        },
                        error: function (xhr, desc, err)
                        {
                            console.log("error: " + desc + " " + err);
                        }
                    });
                }).promise().done(function(){
                    $("#loader").hide();
                    $("#done").show();
                });
                
                
            });

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您使用的是.success 和.done,所以我删除了一个。两者都不需要。此外,为了使这更容易阅读/排除故障,我将后 ajax 代码移动到它自己的函数中。我不知道你为什么要写一个 div 然后再读回来,所以我把它留下了,但把它注释掉了。最后,我添加了一个简单的计数器来迭代您正在进行的后续 ajax 调用,以便它可以检测到一切何时完成。我偷了idea from this post

    $("#loader").show();
    var url = $(this).attr("action");
    var formData = new FormData($(this)[0]);
    $(this).hide();
    $.ajax({
        url: "generateJson.php",
        type: 'POST',
        data: formData,
        async: true,
        cache: false,
        contentType: false,
        enctype: 'multipart/form-data',
        processData: false,
        error: function(xhr, desc, err) {
          console.log("error");
        }
      }).done(function(data) {
        $('#json').append(data); //content loads here
        runPostAjax(data)
      }),
      error: function(xhr, desc, err) {
        console.log("error: " + desc + " " + err);
      }
    });
    
    
    
    function runPostAjax(data) {
      //data = JSON.parse($("#json").val());
      let done = data.length; // number of total requests
      $.each(data, function(key, val) {
            $.ajax({
                type: "POST",
                url: "/generateInvoice.php",
                dataType: "html",
                data: val,
                success: function(data, status) {
                  if (data) {
                    $("#result").append(data)
                    done -= 1;
                    if (done == 0) {
                      $("#loader").hide();
                      $("#done").show();
                    }
                  } else {
                    console.log("Error");
                  }
                })
    
    
            }
    

    【讨论】:

    • 完成 = data.length; // 总请求数 console.log(done);这将返回“未定义”
    • var done = Object.keys(data).length 做到了。
    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 2023-03-25
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多