2010-09-23 71 views
0

这不是工作检查,看是否有阶级存在于一个复选框

$(".gradeA, .gradeU").find(":checkbox").click(function() { 
if (this.checked === false) { return; } 
if (this.hasClass("toggler")) { return; } 

最后一行失败,但我需要检查,看看它这个复选框

<input type="checkbox" name="myCB" value="A" class="toggler" />Couldn't find the Venue<br /> 
+0

我不明白,如果你需要“检查是否它的复选框“,那么上面的”this.checked“行将如何工作如果它不是? – RPM1984 2010-09-23 03:19:54

回答

2

hasClass()jQuery对象的成员方法。因此您需要将this放在$()函数中,否则您试图调用DOM对象上的hasClass()方法,该对象不具有hasClass()作为成员函数。

传递this作为参数传递给jQuery对象(通常简称为$)将返回一个jQuery对象,确实有hasClass()作为成员方法,然后大家都高兴和小精灵可以围着篝火跳一次舞。

if (this.hasClass("toggler")) { return; } //Your Code, wrong. 
if ($(this).hasClass("toggler")) { return; } //My Code, right. 
1

试试这个

$("input[type=checkbox]").each(function(index) { 
    if($(this).attr('class')=='toggler') 
    alert ('yes class is there'); 
    else 
    alert ('no class is not there'); 
}); 

$("input[type=checkbox]").each(function(index) { 
    if ($(this).hasClass("toggler")) { alert("yes class is there"); } 
}); 
0

你也可以检查它使用.is()

$(this).is('.toggler'); 
相关问题