简体   繁体   English

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

[英]Javascript function not getting a value from an input box and loop through an array

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 这是我到目前为止所做的,它不起作用: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\'));

getElementsByName returns an array and your function is expecting a single element, you need to access the first element like this: getElementsByName返回一个数组,你的函数需要一个元素,你需要访问第一个元素,如下所示:

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

Change the ting.value to boxes.length in the for loop: 在for循环中将ting.value更改为box.length:

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

Try using 尝试使用

checked=checked

instead of 代替

checked=true

Try this: 尝试这个:

boxes.item(i).checked = checked

sorry not sure how to insert javascript into the post 抱歉不确定如何在帖子中插入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? 从你的语句“从输入框中收集一个数量”我认为你期望ting是一个输入到文本框中的数字,所以你正在调用一个函数,该函数需要一个值应该解析为表单元素对象数组的数字?

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时确保传递数字,则可能会更好:

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. 如果您发布实际的HTML和实​​际的JavaScript代码,它会有很大帮助。

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. 现在,无论输入字段中键入的值如何,都可以避免出现数组超出范围的错误,并且对checkAmount的调用应该可以工作。

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

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