Javascript函数未从输入框获取值并循环遍历数组

发布于 2024-10-10 22:47:05 字数 424 浏览 2 评论 0原文

我试图从输入框中收集金额,然后勾选一定数量的数组框。

这是我到目前为止所得到的,但它不起作用:S

function checkAmount(ting)  {  
var boxes = document.getElementsByTagName("input");  
for (var i = 0; i < ting.value; i++)   {  
    if (boxes[i].name == "vote[]")   {  
        boxes[i].checked = true;  
    }  
}  
}

我用这个来称呼它:

uncheckAll(); 
checkAmount(document.getElementsByName(\'ammount\'));

im trying to gather an amount from a input box and then tick a certain amount of boxes which is an array.

this is what i got so far and it doesnt work :S

function checkAmount(ting)  {  
var boxes = document.getElementsByTagName("input");  
for (var i = 0; i < ting.value; i++)   {  
    if (boxes[i].name == "vote[]")   {  
        boxes[i].checked = true;  
    }  
}  
}

And im calling it with this:

uncheckAll(); 
checkAmount(document.getElementsByName(\'ammount\'));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

鹿港小镇 2024-10-17 22:47:05

getElementsByName 返回一个数组,并且您的函数需要单个元素,您需要像这样访问第一个元素:

checkAmount(document.getElementsByName(\'ammount\')[0]); 

在 for 循环中将 ting.value 更改为boxs.length:

function checkAmount(ting)  {  
var boxes = document.getElementsByTagName("input");  
for (var i = 0; i < boxes.length; i++)   {  
        boxes[i].checked = (boxes[i].name == "vote[]") ;  
  }  
}

getElementsByName returns an array and your function is expecting a single element, you need to access the first element like this:

checkAmount(document.getElementsByName(\'ammount\')[0]); 

Change the ting.value to boxes.length in the for loop:

function checkAmount(ting)  {  
var boxes = document.getElementsByTagName("input");  
for (var i = 0; i < boxes.length; i++)   {  
        boxes[i].checked = (boxes[i].name == "vote[]") ;  
  }  
}
素手挽清风 2024-10-17 22:47:05

试试这个:

boxes.item(i).checked =Checked

抱歉,不知道如何在帖子中插入 JavaScript

Try this:

boxes.item(i).checked = checked

sorry not sure how to insert javascript into the post

溺ぐ爱和你が 2024-10-17 22:47:05

你发布你的真实代码吗?
如果是这样,您是否意识到函数调用中的“金额”拼写错误?

从您的陈述“从输入框中收集金额”来看,我认为您希望 ting 是输入到文本框中的数字,因此您正在调用一个函数,该函数需要一个具有应解析为表单元素对象数组的值的数字?

var m = document.getElementsByName("amount");

m 将是一个数组!

换句话说,如果您确保在调用 checkAmount: 时传递了一个数字,那么效果可能会更好:

checkAmount(documents.forms[0].amount.value)

或者:

checkAmount(documents.getElementsByName("amount")[0].value)

如果您发布实际的 HTML 和实际的 javascript 代码,将会有很大帮助。

are you posting your real code?
If so, you realize that you are spelling 'amount' wrong in your function call?

From your statement "gather an amount from an input box" I think you expect ting to be a number entered into a text box and so you are calling a function that expects a number with a value that should resolve to an array of form element objects?

var m = document.getElementsByName("amount");

m will be an array!

in other words, it might work better if you made sure you were passing a number when you call checkAmount:

checkAmount(documents.forms[0].amount.value)

or:

checkAmount(documents.getElementsByName("amount")[0].value)

It would help a lot if you post the actual HTML and actual javascript code.

倾城泪 2024-10-17 22:47:05

正如之前的答案和回复所示,您发布的代码存在几个问题。尝试这样的操作:

function checkAmount(ting)  {  
    var boxes = document.getElementsByTagName("input");  
    for (var i = 0; i < ting.value && i < boxes.length; i++)   {  
        if (boxes[i].name == "vote[]")   {  
            boxes[i].checked = true;  
        }  
    }  
}

像这样调用:

uncheckAll(); 
checkAmount(document.getElementsByName(\'ammount\')[0]);

现在,无论在输入字段中键入的值如何,您都可以免受数组越界错误的影响,并且对 checkAmount 的调用应该有效。

There are several problems with the code you posted, as indicated by the previous answers and replies. Try something like this:

function checkAmount(ting)  {  
    var boxes = document.getElementsByTagName("input");  
    for (var i = 0; i < ting.value && i < boxes.length; i++)   {  
        if (boxes[i].name == "vote[]")   {  
            boxes[i].checked = true;  
        }  
    }  
}

Called like this:

uncheckAll(); 
checkAmount(document.getElementsByName(\'ammount\')[0]);

Now you are safe from array out of bounds errors no matter the value typed in the input field and the call to checkAmount should work.

野の 2024-10-17 22:47:05

尝试使用

checked=checked

而不是

checked=true

Try using

checked=checked

instead of

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