2015-04-01 95 views
1

我想获得一个.each语句内的元素的属性。jquery每个获取属性

$(document).ready(function(){ 
    $(':not(select[name=""])').each(function(e) { 
     var el = this.attr(name); 
     alert('el'); 
    }); 
}); 

所以,如果我有两个匹配的元素,那么我希望它提醒两次。有人可以帮助我解决我的问题。谢谢。

+0

变化'this.attr(名称)'这样:'$(本) .attr(“name”)' – CodeGodie 2015-04-01 18:52:28

+0

和'alert('el');'to alert(el);' – 2015-04-01 18:52:59

+0

这样做。谢谢......只是我猜测我的草率代码。 – user982853 2015-04-01 18:57:50

回答

0

您忘记带$()

$(document).ready(function(){ 
    $(':not(select[name=""])').each(function(e) { 
     var el = $(this).attr(name); 
     alert(el); // also output the variable not a char 
}); 
}); 
0

首先来包装选择,this将涉及一个DOMElement,而使用attr()方法,你需要获得一个jQuery对象包含的元素,所以你需要$(this) 。其次,你需要alert(el)没有引号。试试这个:

$(document).ready(function(){ 
    $(':not(select[name=""])').each(function(e) { 
     var el = $(this).attr(name); 
     alert(el); 
    }); 
}); 
0

所有你需要做的是改变this.attr(name)$(this).attr("name")和(如加里·斯托里说的)alert('el');alert(el);