简体   繁体   English

如何更改Javascript中的数组?

[英]How can I change an array in Javascript?

I want to convert numbers (inputted by user) into an array. 我想将数字(由用户输入)转换为数组。 These are the inputs: 这些是输入:

<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.... -->

Of who I have stored the ids in javascript. 谁将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....

I also got these empty arrays to store the inputted numbers: 我还得到了这些空数组来存储输入的数字:

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

Then I use the ids in the first arrays to get the numbers I want to place in the second. 然后,我使用第一个数组中的id来获取要放置在第二个数组中的数字。

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

But this doesn't work. 但这是行不通的。 What is going wrong or is there a way easier solution? 出了什么问题或有没有更简单的解决方法?

You don't need eval and you might need to do a bit of studying on how to optimize your programming logic...but just to get your current code working, you would do something like. 您不需要eval ,您可能需要做一些有关如何优化编程逻辑的研究……但是只要使当前代码正常工作,您就可以做类似的事情。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Javascript 中更改常量数组? - How Can I Change Constant Array in Javascript? 如何将 javascript 中的 object 数组更改为 object 数组? - how can i change array of object to object of array in javascript? JavaScript:如何更改数组中对象的属性名称? - JavaScript: How can I change property names of objects in an array? 如何使按钮更改Javascript数组位置? - How can I make a button change Javascript array positions? 如何在不改变容器的情况下更改数组值(对象数组内部)? javascript - How can I change array value(inside array of objects) without mutating the container? javascript 使用JQuery和javascript单击按钮后,如何更改嵌套对象数组中的值? - How can I change the value in a nested array of objects, after a button click using JQuery and javascript? 如何在不声明其他变量更改的情况下更改javascript数组中的变量? - How can I change a variable in a javascript array without the other variables changes being declared? 如何访问Javascript数组中的元素(div类名),并更改其不透明度? - How can I access elements (div ClassNames) in a Javascript array, and change their opacity? 如果javascript / html中的其他一些值更改,如何更改值的数组? - How can I change my array of values if some other value changes in javascript/html? 如何更改JavaScript的初始值 - How can I change the initial value on JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM