简体   繁体   English

jQuery:对于input元素的每个属性,我如何获得该输入元素的名称?

[英]jQuery: For each attribute of an input element how do I get that input element's name?

I'm looking at each input element that has an attribute named "isDate". 我正在查看每个具有名为“isDate”属性的输入元素。 I want to find that attributes parent input element's name attribute. 我想找到属性父输入元素的name属性。

<input name="A0000" isDate="true" value="01/01/2020" />
<input name="A0001" isDate="true" value="01/01/2021" />
<input name="A0002" isDate="true" value="01/01/2022" />
<input name="A0003" isDate="true" value="01/01/2023" />
<input name="A0004" isDate="true" value="01/01/2024" />
<input name="A0005" isDate="true" value="01/01/2025" />


  $("input[isDate="true"]).each(function(){
     var _this = this;
// do stuff then... 

// get name of input
     var name =  $(_this).parent().attr("name").val(); // this doesn't work
  });

this will reference the element, not the attribute, so you won't need .parent() this将引用元素,而不是属性,因此您不需要.parent()

$("input[isDate='true']").each(function(){
    // get name of input
    var name =  $(this).attr("name");
});

I also fixed some of your syntax too! 我也修改了一些你的语法!

Skip the parent and val part 跳过父级和val级

See here 看到这里

Check out the fiddle: http://jsfiddle.net/SEsBH/ 看看小提琴: http//jsfiddle.net/SEsBH/

  $('input[isDate="true"]').each(function(){
     var _this = this;
// do stuff then... 

// get name of input
     console.log($(this).attr('name'));
  });

You have a wrong " in $("input[isDate . And one missing :) 你有一个错误的"$("input[isDate 。并且一个缺失:)

Also you can just reference the current element using $(this) 你也可以使用$(this)引用当前元素

Is this what you were looking for? 这是你在找什么?

<input name="A0000" isDate="true" value="01/01/2020" />
<input name="A0001" isDate="true" value="01/01/2021" />
<input name="A0002" isDate="true" value="01/01/2022" />
<input name="A0003" isDate="true" value="01/01/2023" />
<input name="A0004" isDate="true" value="01/01/2024" />
<input name="A0005" isDate="true" value="01/01/2025" />


$("input[isDate="true"]).each(function(){
    var _this = this;
    // do stuff then... 

    // get name of input
    var name =  _this.name;
});

Good luck! 祝好运!

暂无
暂无

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

相关问题 当我只知道它的“名称” - 属性时,如何通过jQuery设置input-element的值 - How do I set the value of an input-element via jQuery, when I only know it's “name”-attribute 使用jQuery,当我在输入框之外单击时如何更改输入元素的值? - Using jQuery, how do I change the input element's value when I click outside of the input box? Jquery获取元素属性并将其插入到输入文本中 - Jquery get element attribute and insert it to an input text 按需要的属性验证输入元素,并获取每个输入的标签文本 - Validate input element by required attribute and get the label text for each input 在 jQuery 中,如何通过 name 属性选择元素? - In jQuery, how do I select an element by its name attribute? 如何获得td的值并将其传递给每个td元素的输入? - How do I get the value of td and pass it on input in each td element? 如何使用jQuery获取div中隐藏元素的名称属性? - How do I get the name attribute of a hidden element within a div using jQuery? 如何将angularjs变量值赋给html元素属性,例如输入elememnt的name属性 - How to assign angularjs variable value to html element attribute, such as input elememnt's name attribute 我如何让我的 html 输入元素影响我的 javascript 类对象值(字符名称) - How do i get my html input element to influence my javascript class object value(character name) 如何获取与javascript数组同名的input元素的值? - How do I get values of input element with the same name as an javascript array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM