jQuery 如何使用jQuery获取输入字段的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19304833/
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
How to get name of an input field using jQuery?
提问by Afzaal Ahmad Zeeshan
Creating a plugin in jQuery, I have to get the names of input fields on which events would occur.
在 jQuery 中创建一个插件,我必须获取将发生事件的输入字段的名称。
Suppose:
认为:
<input type="text" name="name" />
<input type="text" name="age" />
Whether submitted or not doesn't matter. What I want is to get the name of the field.
提交与否无所谓。我想要的是获取字段的名称。
I tried the following:
我尝试了以下方法:
$('#ins').html($(this).attr('name').val());
#ins
is the div, where I am writing the events such as a click
was made on the input with name = name
. This way I want to create a plugin which would guide me to get to know which fields are getting some events.
#ins
是 div,我在其中编写事件,例如click
在输入上使用name = name
. 通过这种方式,我想创建一个插件来指导我了解哪些字段正在接收一些事件。
回答by MSMOHAN
You are bit right but when you trying to access attribute you have to use only the attr
not the val()
.
您是位权,但是当你试图访问属性,你必须只使用attr
不val()
。
$(this).attr('name')
The above code is enough to get the name of the event triggered elements name attribute.
上面的代码足以获取事件触发的元素名称属性的名称。
回答by Hugo Tostes
try this:
尝试这个:
$("input[type=text]").each(function(){
$('#ins').html($(this).attr('name') + " <br>");
});
with this code, you will get the name attribute of all input in the page. And i put a "br" just to break a line.
使用此代码,您将获得页面中所有输入的名称属性。我放了一个“br”只是为了换行。