繁体   English   中英

在 javascript 中访问输入的最佳方式是什么?

[英]What is the best way to access input in javascript?

 function gener10k() { let x = getRndInteger(10000, 100); let x1 = getRndInteger(x, 10); let x2 = x - x1; let higherValue = numberToArray(x1); const longiness_higher = higherValue.length; let higherInput = document.getElementsByName("higher[]"); for (var i = 0; i < longiness_higher; i++) { higherInput[i].value = higherValue[i].value; } alert('higherValue ' + x1 + ' ' + higherValue[0] + higherValue[1] + higherValue[2] + higherValue[3] + "higherInput " + higherInput[0] + higherInput[1].value + higherInput[2].value + higherInput[3].value); } function getRndInteger(max, min) { return Math.floor(Math.random() * (max - min)) + min; } function getlength(number) { return number.toString().length; } function numberToArray(number) { let array = number.toString().split(""); return array.map(x => parseInt(x)); }
 <body> <div class="inputCoefs"> <div class="inputCoef1"> <input type="text" name="higher[]" class="coeficientsIn10000" /> <input type="text" name="higher[]" class="coeficientsIn10000" /> <input type="text" name="higher[]" class="coeficientsIn10000" /> <input type="text" name="higher[]" class="coeficientsIn10000" /> <input type="text" name="higher[]" class="coeficientsIn10000" /> </div> <div class="output"> <button class="but" id="checkit" onclick="gener10k();">DO IT</button> </div> </div>

我希望将特定数字写入等效的输入数组。 但相反,我有未定义的变量。 从数组写入输入的最佳方法是什么?

let higherInput = document.getElementsByName("higer[]");

此语句获取 object。

let higherValue = numberToArray(x1);

此语句获取一个数字数组。

在你的for循环中:

higherInput[i].value = higherValue[i].value;

在此语句中, higherInput[i].value指的是您的文本框值,但higherValue[i].value是什么?
只需使用higherValue[i]

您可以使用下面的语句来查看higherValue[i]是一个数字。

console.log(typeof(higherValue[0]);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM