我想将数字(由用户输入)转换为数组。 这些是输入:
<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.... -->
我在javascript中存储了哪些ID。
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'));
但这不起作用。出了什么问题或是否有更方便的解决方案?
答案 0 :(得分:1)
你不需要eval
,你可能需要做一些关于如何优化编程逻辑的研究......但是为了让你当前的代码正常工作,你会做类似的事情。
for(i=0;i<9;i++){
var oldStr = h1[i];
valueH1[i] = document.getElementById(oldStr).value;
};
alert(valueH1.join('\n'));