简体   繁体   English

如何使用名称属性查找输入字段的值?

[英]How to find value of input field using name attribute?

How to access the value of this input field by its name attribute using Javascript 如何使用Javascript通过其名称属性访问此输入字段的值

<input type='text' name='hey'>

document.querySelectorAll('input[name=hey]').value;

You were close, As querySelectorAll() returns a list so you can use indexer to access the elements. 您已经关闭了,因为querySelectorAll()返回一个列表,所以您可以使用indexer来访问元素。

document.querySelectorAll('input[name=hey]')[0].value

better use querySelector() 更好地使用querySelector()

document.querySelector('input[name=hey]').value

You can use 您可以使用

document.getElementsByName(name)

Since you wanted javascript? 既然您想要JavaScript? Or do you want JQuery? 还是您想要JQuery? cheers! 干杯!

If you are flexible to use JQuery the here is the answer. 如果您灵活使用JQuery,那么这里就是答案。

    $("input[name=hey]").val();

Using Javascript, you can access it like this:- 使用Javascript,您可以像这样访问它:-

    document.getElementsByName('key')[0].value;

Look into this jsfiddle:- 看看这个jsfiddle:-

https://jsfiddle.net/pLmvrdf3/ https://jsfiddle.net/pLmvrdf3/

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

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