javascript - JavaScript 没有从 JQuery-AJAX 中捕获正确的列表项
问题描述
我有两个函数 main 函数,其中 JQuery 和另一个在 Javascript 中。
jQuery:
$(document).ready(function () {
$('#ProvisionLink').click(function () {
var queryTargetList = [ "Item1","Item2","Item3","Item4"]
var data = ["Data1","Data2","Data3","Data4" ]
event.preventDefault();
var i;
for(i = 0; i < queryTargetList.length; ++i) {
var dataItem = data[i];
var QueryItems = queryTargetList[i];
console.log("Before Launching the Javascript : " +QueryItems)
$.ajax({
url: '/operation',
data: {DataType: dataItem,},
type: 'POST',
success: function (response){ getInventory(response,QueryItems) },
error: function (error) {console.log(error);}
});
}
});
});
Java脚本:
function getInventory(data,queryTarget){
console.log("In Get Inventory Function in javascript ... " +queryTarget)
var queryTarget = "#"+queryTarget
// get the query id
const SelectBoxQuery = document.querySelector(queryTarget)
console.log(SelectBoxQuery)
// resetting the values to enter all the new one.
SelectBoxQuery.options.length = 0; // reset the values
// making a loop to reach each element item in the list
for (var i = 0; i < data.length; ++i) {
// add new element which is option to the targeted ID
var SelectBoxQuery_Addition = document.createElement('option');
// adding a text to that option
SelectBoxQuery_Addition.text = data[i];
// apply the adding .
SelectBoxQuery.add(SelectBoxQuery_Addition)
}}
问题是,在我得到 python FLASK 对 Jquery 函数的响应之后。在成功部分,它应该使用与 queryTargetList 相同的 i 启动 javascript 函数。
例如,如果我传递数据 [1],我希望也有 queryTargetList [1]。
但在 javascript console.log 函数中。这不会发生。它打印 queryTargetList 列表的最后一项。
打印:
Before Launching the Javascript : Item1
JQueryTests.js:11 Before Launching the Javascript : Item2
JQueryTests.js:11 Before Launching the Javascript : Item3
JQueryTests.js:11 Before Launching the Javascript : Item4
operationJavaScript.js:115 In Get Inventory Function in javascript ... Item4
In Get Inventory Function in javascript ... Item4
In Get Inventory Function in javascript ... Item4
In Get Inventory Function in javascript ... Item4
我不知道做错了什么:(
解决方案
由于 ajax 调用是异步操作,因此在i
调用成功方法时循环变量会发生变化。不能保证i
会到达终点。您需要将变量包装在一个闭包中,以便它的成功方法获得正确的项目。示例代码未经测试。
$(document).ready(function () {
$('#ProvisionLink').click(function () {
var queryTargetList = [ "Item1","Item2","Item3","Item4"]
var data = ["Data1","Data2","Data3","Data4" ]
event.preventDefault();
var i;
for(i = 0; i < queryTargetList.length; ++i) {
var dataItem = data[i];
var QueryItems = queryTargetList[i];
console.log("Before Launching the Javascript : " +QueryItems)
(data,queryItem)=>{
$.ajax({
url: '/operation',
data: {DataType: data,},
type: 'POST',
success: function (response){ getInventory(response,queryItem) },
error: function (error) {console.log(error);}
});
})(dataItem,QueryItems);
}
});
});
推荐阅读
- c++ - 通过将其拆分为线程来加快 for 循环
- c# - 如何在 C# 中读取此 XML?
- python - 使用 TFLiteConverter RuntimeError 将 Tensorflow (TF2) 量化为完整整数错误:操作尚不支持量化:'CUSTOM'
- python-3.x - 使用多处理并行化列表项附加到 dict
- pip - 如何强制 pip 安装编译它的包(无二进制)但使用以前的特定版本
- php - 选择查询未显示预期结果。print_ 仅正确显示第一个数组,然后显示其他行电子邮件字段的单个字符串
- javascript - 如何创建可以接受文本、图片、gif 的多媒体输入字段,例如 Facebook/Twitter“创建帖子”输入字段
- r - 如何使用 renv 显式管理依赖项
- python - 读取多个文本文件并对其进行迭代
- php - Eloquent hasMany 不加载集合