开发者

Checking to see if a class is present on a checkbox

This is not working

$(".gradeA, .gradeU").find(":checkbo开发者_C百科x").click(function() {
if (this.checked === false) { return; }
if (this.hasClass("toggler")) { return; }

The last line is failing but i need to check to see if its this checkbox

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


hasClass() is a member method of the jQuery object. so you need to enclose the this in the $() function, otherwise you are trying to call the hasClass() method on the DOM object, which doesn't have hasClass() as a member function.

Passing this as a parameter to the jQuery object (often shortened to $) will return a jQuery object which does have hasClass() as a member method, and then everyone is happy and pixies can dance around the campfire again.

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


Try this

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

or

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


you could also check it using .is()

$(this).is('.toggler');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜