提问人:Nurin Azmir 提问时间:3/15/2023 更新时间:3/15/2023 访问量:41
for 循环在 jQuery 中自动迭代两次
for loop iterate two time in jquery automatically
问:
我正在尝试通过调用api来获取数据并将数据放入表中。我在对象类型中发现了三个长度的数据。然后,当我想在桌子上显示它时,它显示六行,但我想要三行。当我调试代码时,我注意到 for 循环的索引迭代 0-2(这完全是我想要的),但这个 for 循环第二次调用 auto 并启动索引 0-2(这是我的问题),这就是为什么它可能在我的列中显示 3+3 = 6 行。我不确定确切的问题是什么。如何解决此问题?
这是我的脚本: $(文档).ready(函数() {
$.post('http://localhost/..., function (data) {
var leaveList = data.data;
var keys = Object.keys(leaveList);
var len = keys.length;
//alert(len);
for (var i = 0; i < len; i++) {
if (leaveList[i]["absenceTypeID"] == 'SL') {
var row = '<tr><td> ' + leaveList[i]["absenceTypeID"] + ' </td><td> Sick Leave </td> <td> ' + leaveList[i]["absLimiteDimension"] + ' </td> <td>' + leaveList[i]["calculated"] + '</td> </tr>'
} else if (leaveList[i]["absenceTypeID"] == 'CL') {
var row = '<tr><td> ' + leaveList[i]["absenceTypeID"] + ' </td><td> Casual Leave </td> <td> ' + leaveList[i]["absLimiteDimension"] + ' </td> <td>' + leaveList[i]["calculated"] + '</td> </tr>'
} else if (leaveList[i]["absenceTypeID"] == 'EL') {
var row = '<tr><td> ' + leaveList[i]["absenceTypeID"] + ' </td><td> Earned Leave </td> <td> ' + leaveList[i]["absLimiteDimension"] + ' </td> <td>' + leaveList[i]["calculated"] + '</td> </tr>'
} else if (leaveList[i]["absenceTypeID"] == 'ML') {
var row = '<tr><td> ' + leaveList[i]["absenceTypeID"] + ' </td><td> Maternity Leave </td> <td> ' + leaveList[i]["absLimiteDimension"] + ' </td> <td>' + leaveList[i]["calculated"] + '</td> </tr>'
} else if (leaveList[i]["absenceTypeID"] == 'VL') {
var row = '<tr><td> ' + leaveList[i]["absenceTypeID"] + ' </td><td> Visit Leave </td> <td> ' + leaveList[i]["absLimiteDimension"] + ' </td> <td>' + leaveList[i]["calculated"] + '</td> </tr>'
} else {
var row = '<tr><td> ' + leaveList[i]["absenceTypeID"] + ' </td><td> Earned Leave (Carry) </td> <td> ' + leaveList[i]["absLimiteDimension"] + ' </td> <td>' + leaveList[i]["calculated"] + '</td> </tr>'
}
}
});
});
答: 暂无答案
评论
$.each(leaveList , function( index , obj ){ //console.log(obj); })
for
console.log(leaveList)
<script src="yourfile.js"></script>
/* $.post.. */
<script src="samesrc.js">