从使用 getElementsByClassName() 创建的数组中获取值

发布于 2025-01-05 17:05:06 字数 890 浏览 0 评论 0原文

我正在尝试创建一个函数来检查以确保用户提供的所有输入都是数字。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2025-01-12 17:05:06

也将

var inp = document.getElementsByClassName('userInput'); 

var string= "justToInitializeThis";
for(var i=0; i < inp.length; i++) {
    string = string + " " + inp[i].value;
}
alert(string);

其用于另一个循环

Use this

var inp = document.getElementsByClassName('userInput'); 

var string= "justToInitializeThis";
for(var i=0; i < inp.length; i++) {
    string = string + " " + inp[i].value;
}
alert(string);

Same for another loop too

想你只要分分秒秒 2025-01-12 17:05:06

更改:

for(var m in inp) {
    string = string + " " + inp.value;
}

收件人:

for(var m in inp) {
    string = string + " " + inp[m].value;
}

for ... in 循环迭代对象的属性

https ://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in

Change:

for(var m in inp) {
    string = string + " " + inp.value;
}

To:

for(var m in inp) {
    string = string + " " + inp[m].value;
}

A for ... in loop iterates over the properties of an object

https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文