获取表单对象中动态输入元素的值 [英] Get value of dynamic input element in form object

查看:67
本文介绍了获取表单对象中动态输入元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:如何在表单中引用输入元素的动态名称?

My question is: How do I reference a dynamic 'name' of an input element in a form?

例如,使用以下HTML:

For instance, with the following HTML:

<form>
    <input type="text" name="qty1" value="input1" />
     <input type="text" name="qty2" value="input2" />
     <input type="text" name="qty3" value="input3" />

    <input type="submit" value="Submit" onClick="checkVal(this.form); return false;" />
</form>

Javascript:

Javascript:

function checkVal(form) {
    for (var i = 1; i <= 3; i++) {
        alert(form.qty+i.value);  // Here's where my problem is..
    }
}

以上JavaScript不起作用。警报输出 NaN

The above javascript does not work. The alert is outputting NaN.

如何引用 qty1 qty2 ,以及使用 i 变量在for循环中 qty3

How do I reference qty1, qty2, and qty3 in a for loop using i variable?

这是一个jsfiddle: http://jsfiddle.net/MRzWf/

Here's a jsfiddle: http://jsfiddle.net/MRzWf/

推荐答案

使用 括号内容

 form["qty" + i].value

DEMO

这篇关于获取表单对象中动态输入元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆