简体   繁体   中英

jquery if else statement

What am I doing wrong here? This is on jquery-mobile.

$('.ui-checkbox').click(function() {
if ( 
    $('.ui-checkbox label.ui-checkbox-off').removeClass('off').addClass('on')) {        
}else {
    $('.ui-checkbox label.ui-checkbox-on').removeClass('on').addClass('off');
}
});

The first portion works, but not when I click a 2nd time.

From your comment, you seem to want

$('.ui-checkbox').click(function() {
   $('label.ui-checkbox-on,label.ui-checkbox-off', this)
        .toggleClass('on').toggleClass('off');
});

But this is a little strange. You could use a simple class ( on ) and simply do

$('.ui-checkbox').click(function() {
   $('label', this).toggleClass('on');
});

You probably don't need 2 classes as usually "off" means "not on".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM