迭代相同的表单元素 [英] Iterating through same form elements
本文介绍了迭代相同的表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果表单重复使用相同的标签。
If a form has same tag repeatedly.
如何在javascript中获取其值。
How to get its value in javascript.
<script>
function validate()
{
//get all values of elmone
}
</script>
<form>
<input type="text" size="15" value="Hello World" name="elmOne">
<input type="text" size="15" value="next Hello World" name="elmOne">
<input type="button" onclick="javascript:validate()" value="validate">
</form>
谢谢..
推荐答案
将两个不同的文本输入元素命名为通用名称并不常见。但是,回到问题,你总是可以使用document.getElementByName('elmOne');获取匹配输入的列表,然后遍历它以获取值
it is not a common practice to name two different text input element with a common name. However, back to the question, you can always use document.getElementByName('elmOne'); to get a list of the matching input, then loop through it for value
var input_list = document.getElementsByName('elmOne');
for(var i = 0; i < input_list.length; i++) {
// either way should get you the value
console.debug(input_list[i].value, input_list[i].getAttribute('value'));
}
你可能想参考这个页面了解有关兼容性的更多信息
you may want to refer to this page for further information on compatibility
这篇关于迭代相同的表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文