Javascript 获取输入的名称或 ID,而不是输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7927350/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:05:35  来源:igfitidea点击:

Getting the name or id of an input, not the input value

javascriptjquery

提问by Gabriel

Simple question.

简单的问题。

<input type="text" value="Some text" name="thisname" id="thisid" />

How can I get the name of the id or name that is being used, instead of value. It is clear that value of this text-box is "Some text". But what I want is name of id and name being used now.

如何获取正在使用的 id 或名称的名称,而不是值。很明显,这个文本框的值是“一些文本”。但我想要的是现在使用的 id 和 name 的名称。

To be more clear ... I want "thisid" or "thisname".

更清楚地说......我想要“thisid”或“thisname”。

See my code

看我的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$(document).ready(function() {
    $("p").parent(".selected").css("background", "#FF8877");

});

function add(name) {
    $("#" + name).parent().clone().insertAfter($("#" + name).parent());
}
</script>
</head>

<body>

<div><p>Hello</p></div>
<div class="selected"><p name="name" id="name" onclick="add(this.name)">Hello Again</p></div>
</body>
</html>

Any idea ?

任何的想法 ?

回答by Benjie

So, assuming that you have var inputset to your element:

因此,假设您已var input设置为元素:

  • input.valuegives you the value
  • input.idgives you the id
  • input.namegives you the name
  • input.value给你价值
  • input.id给你身
  • input.name给你名字

回答by Broncha

if you are using jQuery

如果你使用 jQuery

$(this).attr('name'); //for name
$(this).attr('id'); //for ID

回答by clem

if yo use jquery you can do the follwing:

如果您使用 jquery,您可以执行以下操作:

$('#thisid').attr('name');

回答by Roman

Looking at your edited question. You're doing it wrong.

看着你编辑的问题。你这样做是错的。

It'd be much simpler to pass a reference to the element, and not it's name.

传递对元素的引用而不是它的名称会简单得多。

js:

js:

function add(element) {
    $(element).parent().clone().insertAfter($(element).parent());
}

html:

html:

<p name="name" id="name" onclick="add(this)">Hello Again</p>

If you want to know why your code didn't work:

如果您想知道为什么您的代码不起作用:

"id" and "name" are 2 different attributes. In CSS (and therefore in jquery) you generally select an element by ID, with this syntax: #idOfElement. If you really have to, you could select one or more elements by name: *[name="nameOfElement"].

“id”和“name”是两个不同的属性。在 CSS(因此在 jquery)中,您通常按 ID 选择一个元素,语法如下:#idOfElement. 如果确实需要,您可以按名称选择一个或多个元素:*[name="nameOfElement"]

this gives you 2 options:

这为您提供了 2 个选择:

  • pass to your original addfunction the id, not the name:

    <p name="name" id="name" onclick="add(this.id)">Hello Again</p>
    
  • change your add function to work with names instead

    function add(name) {
        $('*[name="' + name + '"]').parent().clone().insertAfter($('*[name="' + name + '"]').parent());
    }
    
  • addid传递给您的原始函数,而不是名称:

    <p name="name" id="name" onclick="add(this.id)">Hello Again</p>
    
  • 更改您的添加函数以使用名称代替

    function add(name) {
        $('*[name="' + name + '"]').parent().clone().insertAfter($('*[name="' + name + '"]').parent());
    }
    

Anyway, passing a reference of the element itself, is still the best option.

无论如何,传递元素本身的引用仍然是最好的选择。