gpt4 book ai didi

javascript - 我似乎无法清空 javascript 数组

转载 作者:行者123 更新时间:2023-11-30 08:27:03 25 4
gpt4 key购买 nike

我知道 Stackoverflow 上有很多这样的问题,但没有一个对我有用。我想发布我自己的代码并尝试找出我做错了什么。

所以,我有一个函数可以将一堆项目添加到数组中。该页面永远不会刷新,因为它是设计的方式。因此,当再次调用相同的函数时,必须清空数组,因为:

  1. 添加到数组的项目链接到用户选择的页面上的特定项目。
  2. 当选择一个新项目时,旧项目的东西不应显示在数组中。只有新项目的东西。
  3. 该数组由复选框值填充,然后该数组用于在弹出模式表单上创建不同的复选框。

这是填充数组的代码:

// Here I tried emptying the array with below mentioned methods. The array is defined outside of the function. I have also tried defining it inside.
var count = 0;
var optionsSelected = 0;
$('input[name="extrasSelected"]:checked').each(function() {
var selected = $(this).val().split("|");
extras[count] = [selected[0], selected[1], selected[2], selected[3], 'checked'];
optionsSelected++;
count++;
});

$('input[name="extrasSelected"]:not(:checked)').each(function() {
var notSelected = $(this).val().split("|");
extras[count] = [notSelected[0], notSelected[1], notSelected[2], notSelected[3], 'notChecked'];
count++;
});

extras.sort();

这一切都完美无缺。除非我选择一个新项目。弹出显示和复选框是用前一个项目的东西创建的,而复选框是用新项目的东西创建的。

我尝试过使用:

extras = [];
extras.pop();
extras.length = 0
while (extras > 0) {
extras.pop();
}

其实stackoverflow和google上看到的方法我都试过了。所以我猜我做错了什么。

如有任何帮助,我们将不胜感激!

编辑

请求的附加代码:

var extras = [];
$("#doLookup").click(function() {
var count = 0;
var optionsSelected = 0;
$('input[name="extrasSelected"]:checked').each(function() {
var selected = $(this).val().split("|");
extras[count] = [selected[0], selected[1], selected[2], selected[3], 'checked'];
//alert(extras[0][1]);
optionsSelected++;
count++;
});

$('input[name="extrasSelected"]:not(:checked)').each(function() {
var notSelected = $(this).val().split("|");
extras[count] = [notSelected[0], notSelected[1], notSelected[2], notSelected[3], 'notChecked'];
//alert(extras[0][1]);
count++;
});

extras.sort();

if (extras.length > 0) {
optionalsJSHead = '<table width="100%"><tr><th style="width: 40%;"><b>Optional Extra</b></th><th style="text-align:right;width:15%"><b>Retail</b></th><th style="text-align:right;width:15%"><b>Trade</b></th><th style="text-align:right;width:15%"><b>Market</b></th><th style="text-align:right"><b>Include</b></th></tr>';
optionalsJSBody = '';
if (parseInt(year) === parseInt(guideYear)) {
for (i = 0; i < extras.length; ++i) {
optionalsJSBody += '<tr><td>'+extras[i][0]+'</td><td align="right">R '+extras[i][1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][2].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][3].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right"><input class="chckDis" type="checkbox" name="extrasSelectedAdjust" id="'+ extras[i][0] +'" value="'+ extras[i][0] +'|'+ extras[i][1] +'|'+ extras[i][2] +'|'+ extras[i][3] +'" disabled="disabled"/></td></tr>';
}
} else {
for (i = 0; i < extras.length; ++i) {
if (extras[i][4] == 'notChecked') {
optionalsJSBody += '<tr><td>'+extras[i][0]+'</td><td align="right">R '+extras[i][1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][2].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][3].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right"><input class="chckDis" type="checkbox" name="extrasSelectedAdjust" id="'+ extras[i][0] +'" value="'+ extras[i][0] +'|'+ extras[i][1] +'|'+ extras[i][2] +'|'+ extras[i][3] +'"/></td></tr>';
} else {
optionalsJSBody += '<tr><td>'+extras[i][0]+'</td><td align="right">R '+extras[i][1].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][2].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right">R '+extras[i][3].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")+'</td><td align="right"><input class="chckDis" type="checkbox" name="extrasSelectedAdjust" id="'+ extras[i][0] +'" value="'+ extras[i][0] +'|'+ extras[i][1] +'|'+ extras[i][2] +'|'+ extras[i][3] +'" checked /></td></tr>';
}
}
}
optionalFooter = '</table>';
optionalsJS = optionalsJSHead + optionalsJSBody + optionalFooter;
}
});

最佳答案

根据我的理解,您可以创建一个临时数组,用新值填充它,然后将它分配给 extras 变量。请看下面的代码:

var extras = []; // actual array whose items you want to use

$("#doLookup").click(function() {
var tmpExtras = []; // temporary array to get the new items you want.
var count = 0;
var optionsSelected = 0;
$('input[name="extrasSelected"]:checked').each(function() {
var selected = $(this).val().split("|");
tmpExtras[count] = [selected[0], selected[1], selected[2], selected[3], 'checked'];
//alert(extras[0][1]);
optionsSelected++;
count++;
});

$('input[name="extrasSelected"]:not(:checked)').each(function() {
var notSelected = $(this).val().split("|");
tmpExtras[count] = [notSelected[0], notSelected[1], notSelected[2], notSelected[3], 'notChecked'];
//alert(extras[0][1]);
count++;
});

tmpExtras.sort();

// now assign tmpExtras to extras array, so that
// you can get the new items
extras = tmpExtras;


// ... rest of code that uses extras

关于javascript - 我似乎无法清空 javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com