将数组值附加到字符串

时间:2018-05-25 16:17:16

标签: javascript

如何将数组值(可以是小于20K的任何数字)附加到字符串? 我想模仿:

  

if(form1.EActQ2 0 === undefined){Q2B 0 = 0; }       else(Q2B 0 = document.form1.EActQ2 0 .value);

wicket-auth-roles

///修改后的代码

var und = x < 20000;
var less1 = form1.EActQ1.und.toString();
var less2 = form1.EActQ2.und.toString();
var less3 = form1.EActQ3.und.toString();
var less4 = form1.EActQ4.und.toString();

if (less1 === undefined) { Q1B.und.toString() = 0; }
else (Q1B.und.toString() = document.less1.value); /// is now a value to get added in  an equation

if (less2 === undefined) { Q2B.und.toString() = 0; }
else (Q2B.und.toString() = document.less2.value);

if (less3 === undefined) { Q3B.und.toString() = 0; }
else (Q3B.und.toString() = document.less3.value);

if (less4 === undefined) { Q4B.und.toString() = 0; }
else (Q4B.und.toString() = document.less4.value);

返回NaN。我正在根据它们的值定义单元格并尝试将它们相加。

1 个答案:

答案 0 :(得分:0)

您似乎在寻找bracket notation

var und = 0; // some number below 2000
var less = form1["EActQ" + und];

还使用对象来收集所有这些值,而不是一组Q2B…变量。或者甚至更好,只是一个数组:

arr = [];
…
if (less === undefined) {
  arr[und] = 0;
} else {
  arr[und] = document["less" + und].value;
}