首页 > 解决方案 > JQuery 触发包含 Ajax 发布请求的点击功能

问题描述

我的页面中有两个按钮:

> Button A: btn_trendyolStocks
> Button B: btn_trendyolStocksSYNC

Button A当您提供以下参数时,会一一更新股票:Product Name, Size, And Color

现在我正在尝试button B循环上面的button A's点击功能,并批量更新股票。

问题是当我单击时Button B,for循环立即运行而无需等待Button A's单击功能,因此它是同步工作的。似乎所有的ajax请求都是同时执行的。我希望我的Button B循环等到 ajax 请求完成。(不设置异步:false)。

这里的代码:

  jQuery(".btn_trendyolStocksUpdate").click(function(e,product,color,size) {

    stockCount = jQuery(this).val();
    price = jQuery('#inp_trendyolPrice').val();

    if(typeof product !== 'undefined')
    {
      dataObj = {
        rowCount: rowCount,
        product: product,
        size:size,
        color:color,
        stockCount: stockCount,
      };
    }
    else
    {
      dataObj = {
        rowCount: rowCount,
        product: allProducts[jQuery( "#trendyolProduct" ).val()],
        size:jQuery( "#trendyolSize" ).val(),
        color:jQuery( "#trendyolColor" ).val(),
        stockCount: stockCount,
      };
    }

    if(jQuery('#inp_trendyolPrice').val()){
      dataObj["price"] = price;
    }

    jQuery.ajax({
           type: "POST",
           url: "/wp-content/plugins/promc/templates/updateStock.php",
           dataType: 'json',
           data: dataObj,
           success: function(data)
           {
             jQuery("#trendyolMonitor").append(data.Sql);
             jQuery("#trendyolMonitor").append("<br>");

             if(data.numberOfRows == 10000)
             {
                rowCount = rowCount + 10000;
                if(price > 0)
                {
                  jQuery('#btn_trendyolUpdatePrice').trigger('click');
                }
                else{
                  if(stockCount > 0){jQuery("#btn_trendyolStocksON").trigger('click');}
                  else if(typeof product !== 'undefined')
                  {
                    jQuery('#btn_trendyolStocksOFF').trigger('click',[product,size,color]);
                  }
                  else{
                    jQuery("#btn_trendyolStocksOFF").trigger('click');
                  }
                }
             }
             else
             {
               jQuery("#trendyolMonitor").append("<h3>Tamamlandı!</h3>");
               rowCount = 0;
             }
           },
           error: function(XMLHttpRequest, textStatus, errorThrown)
           {
             alert(XMLHttpRequest.responseText);
           }
      });
    });
  jQuery("#btn_trendyolStocksSYNC").click(function() {
    //Runs Through Some Arrays, and Gets All Out Of Stock Product Names, Colors, and Sizes.
    for(var product in all){
      var colors = all[product];
      for(var singleColor in colors[0]){
        var size = colors[0][singleColor];
        for(var index in size){
          var singleSize = size[index];
          //alert(allProducts[product] + " " + singleColor + " " + singleSize);
          jQuery('#btn_trendyolStocksOFF').trigger('click',[allProducts[product],singleColor,singleSize]);
        }
      }
    }
  });

标签: javascriptphpwordpressasynchronoussynchronization

解决方案


推荐阅读