开发者

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

开发者 https://www.devze.com 2023-02-25 01:37 出处:网络
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.

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.

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 th开发者_JAVA技巧at contains the name attribute of the form element?

document.formname.inputname


I don't mean to sound complicated, and I am looking forward to a reply,

-An inquisitive Web Developer


Hmm, I think you could just use this:

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

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

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

精彩评论

暂无评论...
验证码 换一张
取 消