jQuery 获取 <input> 的属性名称值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3513809/
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
Get attribute name value of <input>
提问by Kaartz
How to get the attribute name value of a input tag using jQuery. Please help.
如何使用 jQuery 获取输入标签的属性名称值。请帮忙。
<input name='xxxxx' value=1>
回答by djdd87
回答by Sarfraz
回答by Laura Riera
var value_input = $("input[name*='xxxx']").val();
回答by Niet the Dark Absol
While there is no denying that jQuery is a powerful tool, it is a really bad idea to use it for such a trivial operation as "get an element's attribute value".
虽然不可否认 jQuery 是一个强大的工具,但将它用于“获取元素的属性值”这样的微不足道的操作是一个非常糟糕的主意。
Judging by the current accepted answer, I am going to assume that you were able to add an ID attribute to your element and use that to select it.
从当前接受的答案来看,我将假设您能够向元素添加 ID 属性并使用它来选择它。
With that in mind, here are two pieces of code. First, the code given to you in the Accepted Answer:
考虑到这一点,这里有两段代码。首先,在接受的答案中给你的代码:
$("#ID").attr("name");
And second, the Vanilla JSversion of it:
其次,它的Vanilla JS版本:
document.getElementById('ID').getAttribute("name");
My results:
我的结果:
- jQuery: 300k operations / second
- JavaScript: 11,000k operations / second
- jQuery:30 万次操作/秒
- JavaScript:11,000k 次操作/秒
You can test for yourself here. The "plain JavaScript" vesion is over 35 times fasterthan the jQuery version.
您可以在这里亲自测试。“纯 JavaScript”版本比 jQuery 版本快 35 倍以上。
Now, that's just for one operation, over time you will have more and more stuff going on in your code. Perhaps for something particularly advanced, the optimal "pure JavaScript" solution would take one second to run. The jQuery version might take 30 seconds to a whole minute! That's huge! People aren't going to sit around for that. Even the browser will get bored and offer you the option to kill the webpage for taking too long!
现在,这只是一个操作,随着时间的推移,您的代码中将有越来越多的内容。也许对于一些特别先进的东西,最佳的“纯 JavaScript”解决方案需要一秒钟才能运行。jQuery 版本可能需要 30 秒到一整分钟!那是巨大的!人们不会因此坐视不理。甚至浏览器也会感到无聊,并为您提供杀死网页的选项,因为它花费了太长时间!
As I said, jQuery is a powerful tool, but it should notbe considered the answer to everything.
正如我所说,jQuery 是一个强大的工具,但不应将其视为解决所有问题的答案。
回答by meagar
You need to write a selector which selects the correct <input>
first. Ideally you use the element's ID $('#element_id')
, failing that the ID of it's container $('#container_id input')
, or the element's class $('input.class_name')
.
您需要编写一个选择器,它<input>
首先选择正确的。理想情况下,您使用元素的 ID $('#element_id')
,而不是容器的 ID$('#container_id input')
或元素的类$('input.class_name')
。
Your element has none of these and no context, so it's hard to tell you how to select it.
您的元素既没有这些也没有上下文,因此很难告诉您如何选择它。
Once you have figured out the proper selector, you'd use the attrmethod to access the element's attributes. To get the name, you'd use $(selector).attr('name')
which would return (in your example) 'xxxxx'
.
一旦你找到了合适的选择器,你就可以使用attr方法来访问元素的属性。要获取名称,您将使用$(selector).attr('name')
which will return (in your example) 'xxxxx'
。
回答by Mikeys4u
A better way could be using 'this', it takes whatever the name of the 'id' is and uses that. As long as you add the class name called 'mytarget'.
更好的方法可能是使用“this”,它采用“id”的任何名称并使用它。只要添加名为“mytarget”的类名。
Whenever any of the fields that have target change then it will show an alert box with the name of that field. Just cut and past whole script for it to work!
每当任何具有目标的字段发生更改时,它都会显示一个带有该字段名称的警报框。只需剪切并过去整个脚本即可工作!
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('.mytarget').change(function() {
var name1 = $(this).attr("name");
alert(name1);
});
});
</script>
Name: <input type="text" name="myname" id="myname" class="mytarget"><br />
Age: <input type="text" name="myage" id="myage" class="mytarget"><br />
回答by Paddy
var theName;
theName = $("input selector goes here").attr("name");
回答by nirali
using value get input name
使用值获取输入名称
$('input[value="1"]').attr('name');
using id get input name
使用 id 获取输入名称
$('input[class="id"]').attr('name');
$('#id').attr('name');
using class get input name
使用类获取输入名称
$('input[value="classname"]').attr('name');
$('.classname').attr('name'); //if classname have unique value
回答by Antonio Correia
If you're dealing with a single elementpreferably you should use the id
selector as stated on GenericTypeTea answer and get the name like $("#id").attr("name");
.
如果您正在处理单个元素,则最好使用id
GenericTypeTea 答案中所述的选择器,并获得类似$("#id").attr("name");
.
But if you want, as I did when I found this question, a list with all the input namesof a specific class (in my example a list with the names of all unchecked checkboxes with .selectable-checkbox
class) you could do something like:
但是,如果你愿意,就像我发现这个问题时所做的那样,一个包含特定类的所有输入名称的列表(在我的示例中是一个包含所有未选中的.selectable-checkbox
类复选框名称的列表),您可以执行以下操作:
var listOfUnchecked = $('.selectable-checkbox:unchecked').toArray().map(x=>x.name);
or
或者
var listOfUnchecked = [];
$('.selectable-checkbox:unchecked').each(function () {
listOfUnchecked.push($(this).attr('name'));
});
回答by Sheetal Mehra
Pass class to input tag and access the name value by using class.
将类传递给输入标签并使用类访问名称值。
<input class="test-class" name='xxxxx' value=1>
<script>
$('.test-class').attr('name');
</script>
Or you can also access the value using id.
或者您也可以使用 id 访问该值。
<input id="test-id" name='xxxxx' value=1>
<script>
$('#test-id').attr('name');
</script>