gpt4 book ai didi

javascript - 将值存储在 JavaScript 数组中

转载 作者:行者123 更新时间:2023-11-28 09:50:30 25 4
gpt4 key购买 nike

我正在定义一个 JavaScript 数组,该数组将保存正在单击的复选框的行号和列号,如果选中复选框,我会将行号和列号的值分配给 1,否则我会将0.

我需要根据复选框的选中和取消选中进行一些操作,并将 1,0 存储在数据库中。

这就是我正在尝试做的事情:

var sampleArray=new Array();

function setAll(column, value) {

var i, n = column.length;
for (i = 0; i <= n; ++i) {
column[i] = value;
}
}

if (//some condition){
if(cell is checked)
setAll(sampleArray,1); // here i am calling a function and setting the value in index to 1;
} else if (cell is unchecked)
{
setAll(sampleArray,0);
}

上述代码的问题是它没有存储所有操作的值(选中和取消选中)。

有人可以帮我解决这个问题吗?是否可以在 JavaScript 中创建 hashmap 类型的函数?

H<key, value>

其中键为列号和行号,值为 0 或 1。

最佳答案

function setAll(column, value) {

var i, n = column.length;
for (i = 0; i <= n; ++i) {
column[i] = value;
}
return (column)
}

if (//some condition){
if(cell is checked)
sampleArray= setAll(sampleArray,1); // here i am calling a function and setting the value in index to 1;
} else if (cell is unchecked)
{
sampleArray= setAll(sampleArray,0);
}

或者如果你想使用全局变量

function setAll(value) {

var i, n = sampleArray.length;
for (i = 0; i <= n; ++i) {
sampleArray[i] = value;
}
}

if (//some condition){
if(cell is checked)
setAll(1); // here i am calling a function and setting the value in index to 1;
} else if (cell is unchecked)
{
setAll(0);
}

要检查复选框是否已选中,请查看 Javascript to check whether a checkbox is being checked or unchecked

关于javascript - 将值存储在 JavaScript 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11120862/

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