javascript - 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]);
}
}
}
});
解决方案
推荐阅读
- javascript - JavaScript:从 Icons 渲染图标时出错 - Material Design
- php - 查询多个 WHERE 和 CASE PHP(CodeIgniter) MySQL
- git - 带有压缩档案的 Git
- python - 有没有办法让python请求通过用户输入来禁用ssl验证?
- git - 如何强制 git rebase 并覆盖合并冲突
- web2py - web2py 是否将“en”翻译成“en”
- python - 获取 AttributeError:模块“pandas”在调用方法“访问 OutbreakLocation 数据”时没有属性“json_normalize”
- ruby-on-rails - 在 capistrano 部署到Passenger / Apache env 后,Rails5 应用程序丢失会话,在 Rails 4 上运行良好
- reactjs - 处理从 React 应用程序到 NodeJS 后端的长时间运行的发布请求时的建议
- php - 路由/API 约定问题