[英]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! 我也修改了一些你的语法!
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.