开发者

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

开发者 https://www.devze.com 2023-03-13 23:52 出处:网络
I\'m looking at each input element that has an attribute named \"isDate\".I want to find that attributes parent input element\'s name attribute.

I'm looking at each input element that has an attribute named "isDate". I want to find that attributes parent input element's name attribute.

<input name="A0000" isDate="true" value="01/01/2020" />
<input name="A0001" isDate="true" value="01/01/2021" />
&开发者_C百科lt;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()

$("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

See here


Check out the fiddle: 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 :)

Also you can just reference the current element using $(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!

0

精彩评论

暂无评论...
验证码 换一张
取 消