简体   繁体   English

说明:for 循环上的 Ajax

[英]Explanation: Ajax on for-loop

Ajax isn't working on a for loop. Ajax 不在 for 循环上工作。 Can someone correct my code and please explain it to me.有人可以更正我的代码,请向我解释。 I just want to put those value on my database.我只想将这些值放在我的数据库中。 I can only make it work if I call ajax once not like this, it is in a for loop.如果我不是这样调用 ajax 一次,我只能让它工作,它在一个 for 循环中。

var counter = $("input[name^= 'quantity']").length;
var array1 = $("input[name^= 'quantity']");
var array2 = $("input[name^= 'unit']");
var array3 = $("input[name^= 'item_description']");
var array4 = $("input[name^= 'stock_no']");
var array5 = $("input[name^= 'eunitcost']");
var array6 = $("input[name^= 'ecost']");

var i;

for (i = 0; i < counter; i++) {
  $.ajax({
    url: 'http://localhost/pm/admin/service/user-service.php',
    type: 'POST',
    dataType: 'json',
    data: {
      operation: 'pr-items',
      pr_no: $('#prno').val(),
      quantity: array1.eq(i).val(),
      unit: array2.eq(i).val(),
      item_description: array3.eq(i).val(),
      stock_no: array4.eq(i).val(),
      eunitcost: array5.eq(i).val(),
      ecost: array6.eq(i).val
    },

    success: function(data) {
      alert('pr items success');
      //todo
    },
    error: function(data) {
      // alert('pr items error');
      //todo
    }
  });
}

I found out that the reason why my code above isnt working because ecost: array6.eq(i).val doesnt have a () after the .val and also I found out that this is not a good practice that having a ajax calls on loops.我发现我上面的代码不工作的原因是ecost: array6.eq(i).val.val之后没有()并且我发现这不是一个好的做法,有一个 ajax 调用循环。 Im sorry if I can't explain well here in stackoverflow that resulting on downvotes and having a temporary ban.抱歉,如果我不能在 stackoverflow 中很好地解释导致downvotes 和临时禁止。 But well I deserve it.但我应得的。 I just can't improve my questions.我只是无法改善我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM