javascript - 使用输入变量和表单名称来引用输入元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5665673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:57:48  来源:igfitidea点击:

javascript - use variables of input and form names to reference input element

javascripthtmlformsvariables

提问by Web_Designer

I have a two variables. One containing the value of the `name` attribute of an input element, and another containing the value of the name attribute of its form element.

我有两个变量。一个包含输入元素的 `name` 属性值,另一个包含其表单元素的 name 属性值。

Using JavaScript how do I reference that input element just like below, except for replacing the inputname with the value of the variable that contains the name attribute of the input element, and the formname with the value of the variable that contains the name attribute of the form element?

使用 JavaScript 我如何像下面一样引用该输入元素,除了将 inputname 替换为包含输入元素的 name 属性的变量的值,并将 formname 替换为包含输入元素的 name 属性的变量的值表单元素?

document.formname.inputname



我的意思不是听起来很复杂,我期待着回复,

-An inquisitive Web Developer

- 一个好奇的 Web 开发人员

回答by Blender

Hmm, I think you could just use this:

嗯,我想你可以用这个:

document.forms['nameOfForm']['nameOfFormElement'];

And to get the value of the elements, just append .valueto the end:

要获取元素的值,只需附加.value到末尾:

document.forms['nameOfForm']['nameOfFormElement'].value;