获取的属性名值

时间:2022-11-27 15:36:26

How to get the attribute name value of a input tag using jQuery. Please help.

如何使用jQuery获取输入标记的属性名值。请帮助。

<input name='xxxxx' value=1>

8 个解决方案

#1


203  

Give your input an ID and use the attr method:

提供输入ID并使用attr方法:

var name = $("#id").attr("name");

#2


29  

Use the attr method of jQuery like this:

使用jQuery的attr方法如下:

alert($('input').attr('name'));

Note that you can also use attr to set the attribute values by specifying second argument:

注意,您还可以通过指定第二个参数来使用attr设置属性值:

$('input').attr('name', 'new_name')

#3


11  

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 JS version of it:

第二,它的香草JS版本:

document.getElementById('ID').getAttribute("name");

My results:

我的结果:

  • jQuery: 300k operations / second
  • jQuery: 300k操作/秒
  • JavaScript: 11,000k operations / second
  • JavaScript: 11000k操作/秒

You can test for yourself here. The "plain JavaScript" vesion is over 35 times faster than the jQuery version.

你可以在这里自己测试。“纯JavaScript”vesion的速度是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 not be considered the answer to everything.

正如我所说,jQuery是一个强大的工具,但是它不应该被认为是解决所有问题的答案。

#4


10  

var value_input = $("input[name*='xxxx']").val();

#5


7  

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').

您需要编写一个选择器,它首先选择正确的。理想情况下,您可以使用元素的ID $('#element_id'),如果不使用元素的ID $(' ' container_id输入'),或者使用元素的类$('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 attr method to access the element's attributes. To get the name, you'd use $(selector).attr('name') which would return (in your example) 'xxxxx'.

一旦找到合适的选择器,就可以使用attr方法来访问元素的属性。要获取名称,您需要使用$(选择器).attr('name'),它将返回(在您的示例中)“xxxxx”。

#6


4  

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 />

#7


1  

var theName;

theName = $("input selector goes here").attr("name");

#8


-2  

For the below line, Initially faced problem with out giving single code that 'currnetRowAppName'value is not taking space with string. So, after that giving single code '"+row.applicationName+"', its taking space also.

对于下面这一行,最初遇到的问题是给出一个“currnetRowAppName”值不占用字符串空间的代码。所以,在这之后,给出一个代码的“+行”。应用名称+"',它也占空间。

Example:

例子:

<button class='btn btn-primary btn-xs appDelete' type='button' currnetRowAppName='"+row.applicationName+"' id="+row.id+ >

var viewAppNAME = $(this).attr("currnetRowAppName");
alert(viewAppNAME)

This is working fine.

这是工作正常。

#1


203  

Give your input an ID and use the attr method:

提供输入ID并使用attr方法:

var name = $("#id").attr("name");

#2


29  

Use the attr method of jQuery like this:

使用jQuery的attr方法如下:

alert($('input').attr('name'));

Note that you can also use attr to set the attribute values by specifying second argument:

注意,您还可以通过指定第二个参数来使用attr设置属性值:

$('input').attr('name', 'new_name')

#3


11  

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 JS version of it:

第二,它的香草JS版本:

document.getElementById('ID').getAttribute("name");

My results:

我的结果:

  • jQuery: 300k operations / second
  • jQuery: 300k操作/秒
  • JavaScript: 11,000k operations / second
  • JavaScript: 11000k操作/秒

You can test for yourself here. The "plain JavaScript" vesion is over 35 times faster than the jQuery version.

你可以在这里自己测试。“纯JavaScript”vesion的速度是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 not be considered the answer to everything.

正如我所说,jQuery是一个强大的工具,但是它不应该被认为是解决所有问题的答案。

#4


10  

var value_input = $("input[name*='xxxx']").val();

#5


7  

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').

您需要编写一个选择器,它首先选择正确的。理想情况下,您可以使用元素的ID $('#element_id'),如果不使用元素的ID $(' ' container_id输入'),或者使用元素的类$('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 attr method to access the element's attributes. To get the name, you'd use $(selector).attr('name') which would return (in your example) 'xxxxx'.

一旦找到合适的选择器,就可以使用attr方法来访问元素的属性。要获取名称,您需要使用$(选择器).attr('name'),它将返回(在您的示例中)“xxxxx”。

#6


4  

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 />

#7


1  

var theName;

theName = $("input selector goes here").attr("name");

#8


-2  

For the below line, Initially faced problem with out giving single code that 'currnetRowAppName'value is not taking space with string. So, after that giving single code '"+row.applicationName+"', its taking space also.

对于下面这一行,最初遇到的问题是给出一个“currnetRowAppName”值不占用字符串空间的代码。所以,在这之后,给出一个代码的“+行”。应用名称+"',它也占空间。

Example:

例子:

<button class='btn btn-primary btn-xs appDelete' type='button' currnetRowAppName='"+row.applicationName+"' id="+row.id+ >

var viewAppNAME = $(this).attr("currnetRowAppName");
alert(viewAppNAME)

This is working fine.

这是工作正常。