Jquery 检查复选框上是否存在类
这不起作用Jquery 检查复选框上是否存在类,jquery,Jquery,这不起作用 $(".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 fin
$(".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 />
找不到场地
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.
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.
试试这个
$("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).attr('class')=='toggler')
alert ('yes class is there');
else
alert ('no class is not there');
});
或
您也可以使用
您也可以使用
我不明白,如果你需要“检查是否是复选框”,那么上面的“this.checked”行如果不是如何工作?我不明白,如果你需要“检查是否是复选框”,那么上面的“this.checked”行如果不是如何工作?