从使用 getElementsByClassName() 创建的数组中获取值
我正在尝试创建一个函数来检查以确保用户提供的所有输入都是数字。
function wholeFormValid() {
var inp = document.getElementsByClassName('userInput');
//userInput is a class name i provide to all my non-hidden input fields.
//I have over 20 hidden values (I need to use hidden values to store state in a session).
//Wanted to print out what my function is getting. I keep seeing undefined values.
var string= "justToInitializeThis";
for(var m in inp) {
string = string + " " + inp.value;
}
alert(string);
//Actual function that will be used once I track down the bug.
for(var i in inp) {
if(inp.value != "") {
if(!(/^[0-9]+$/.test(inp.value))) {
return false;
}
}
}
return true;
该函数
确实返回了正确的输入字段,我可以从不同的页面看出,它们在用户可以提供的输入量方面有所不同。但我无法理解的是为什么我的所有值都返回为 null 而不是用户输入的值。我对 HTML 和 HTML 还很陌生。 Javascript,只需要第二双眼睛就可以了:) 提前致谢。
I'm trying to create a function that will check to make sure all the input a user provided is numeric.
function wholeFormValid() {
var inp = document.getElementsByClassName('userInput');
//userInput is a class name i provide to all my non-hidden input fields.
//I have over 20 hidden values (I need to use hidden values to store state in a session).
//Wanted to print out what my function is getting. I keep seeing undefined values.
var string= "justToInitializeThis";
for(var m in inp) {
string = string + " " + inp.value;
}
alert(string);
//Actual function that will be used once I track down the bug.
for(var i in inp) {
if(inp.value != "") {
if(!(/^[0-9]+$/.test(inp.value))) {
return false;
}
}
}
return true;
}
The function does get the right input fields back, I can tell from my different pages and they vary in the amount of input a user can give. But what i can't understand is why all my values are returned as null instead of what the user entered. I'm fairly new to HTML & Javascript and just needed a second pair of eyes on this :) Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也将
其用于另一个循环
Use this
Same for another loop too
更改:
收件人:
https ://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in
Change:
To:
https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in