gpt4 book ai didi

javascript - 如何更改 Javascript 中的数组?

转载 作者:行者123 更新时间:2023-12-03 02:46:39 25 4
gpt4 key购买 nike

我想将数字(由用户输入)转换为数组。这些是输入:

<input id="x1_y1" class="v x1 y1">
<input id="x2_y1" class="v x2 y1">
<input id="x3_y1" class="v x3 y1">
<input id="x4_y1" class="v x4 y1">
<input id="x5_y1" class="v x5 y1">
<input id="x6_y1" class="v x6 y1">
<input id="x7_y1" class="v x7 y1">
<input id="x8_y1" class="v x8 y1">
<input id="x9_y1" class="v x9 y1">

<input id="x1_y2" class="v x1 y2">
<input id="x2_y2" class="v x2 y2">
<input id="x3_y2" class="v x3 y2">
<input id="x4_y2" class="v x4 y2">
<input id="x5_y2" class="v x5 y2">
<input id="x6_y2" class="v x6 y2">
<input id="x7_y2" class="v x7 y2">
<input id="x8_y2" class="v x8 y2">
<input id="x9_y2" class="v x9 y2">

<input id="x1_y3" class="v x1 y3">
<input id="x2_y3" class="v x2 y3">
<input id="x3_y3" class="v x3 y3">
<!-- etc.... -->

我已将 ID 存储在 JavaScript 中。

h1 = ["x1_y1","x2_y1","x3_y1","x1_y2","x2_y2","x3_y2","x1_y3","x2_y3","x3_y3"];
h2 = ["x4_y1","x5_y1","x6_y1","x4_y2","x5_y2","x6_y2","x4_y3","x5_y3","x6_y3"];
h3 = ["x7_y1","x8_y1","x9_y1","x7_y2","x8_y2","x9_y2","x7_y3","x8_y3","x9_y3"];
h4 = ["x1_y4","x2_y4","x3_y4","x1_y5","x2_y5","x3_y5","x1_y6","x2_y6","x3_y6"];
h5 = ["x4_y4","x5_y4","x6_y4","x4_y5","x5_y5","x6_y5","x4_y6","x5_y6","x6_y6"];
// etc....

我还得到了这些空数组来存储输入的数字:

valueH1=["","","","","","","","",""];
valueH2=["","","","","","","","",""];
valueH3=["","","","","","","","",""];
valueH4=["","","","","","","","",""];
valueH5=["","","","","","","","",""];
// etc....

然后我使用第一个数组中的 id 来获取要放入第二个数组中的数字。

for(i=0;i<9;i++){
var oldStr = "h1["+i+"]";
eval("valueH1["+i+"]") = document.getElementById(eval(oldStr)).value;
};
alert(valueH1.join('\n'));

但这行不通。出了什么问题或者有更简单的解决方案吗?

最佳答案

你不需要eval,你可能需要做一些关于如何优化你的编程逻辑的研究......但只是为了让你当前的代码工作,你会做类似的事情.

for(i=0;i<9;i++){
var oldStr = h1[i];
valueH1[i] = document.getElementById(oldStr).value;
};
alert(valueH1.join('\n'));

关于javascript - 如何更改 Javascript 中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48067591/

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