简体   繁体   中英

Jquery addClass() , removeClass() functions does not work

$('.push').each(function(){
  if($(':first-child',this).hasClass( "activex" )){
    $(this).off().off('click').on('click',function(){
      var a = $(this).attr('id');
      $.ajax({
        type: "POST",
        url: "includes/rcart.php",
        data:{'pid': a},
        success: function(data){
          var a= parseInt($('.cart').text());
          if((data.indexOf("2")) >= 0){
            console.log(data);
            console.log('not removed'); 
          }else{
            a--;
            console.log('removed');
            $(this).children().removeClass('activex');
            $('.cart').text(a);
          }
        }
      });
      console.log(a); 
    });
  }else{
    $(this).on('click',function(){
      var a = $(this).attr('id');
      $.ajax({
        type: "POST",
        url: "includes/cart.php",
        data:{'pid': a},
        success: function(data){
          console.log(data);
          var a= parseInt($('.cart').text());
          if((data.indexOf("2")) >= 0){
            console.log('done'); 
          }else{
            a++;
            $('.cart').text(a);
            $(this).children().addClass('activex');
          }
        }
      });
      console.log(a); 
    });
  }
});

I am trying to remove the class activex whenever the button with class push has been clicked but its not working and there's no error in the code and it's not removing the class. If i remove the class manually in the chrome console with class .push it works.

I am trying to remove the class activex whenever the button with class push has been clicked but its not working and there's no error in the code and it's not removing the class. If i remove the class manually in the chrome console with class .push it works.

Here is your updated code, just i have removed children() from your code. let me know if it helped or not.

$('.push').each(function(){


      if($(':first-child',this).hasClass( "activex" )){
          $(this).off().off('click').on('click',function(){
        var a = $(this).attr('id');
                $.ajax({

    type: "POST",
    url: "includes/rcart.php",
    data:{'pid': a},
    success: function(data){


     var a= parseInt($('.cart').text());

     if((data.indexOf("2")) >= 0){
          console.log(data);
         console.log('not removed'); 
     }
     else {
     a--;
     console.log('removed');
     $(this).removeClass('activex');
     $('.cart').text(a);
     }
    }

    });

        console.log(a); 
    });

      }

      else {

    $(this).on('click',function(){
        var a = $(this).attr('id');
                $.ajax({

    type: "POST",
    url: "includes/cart.php",
    data:{'pid': a},
    success: function(data){

    console.log(data);
     var a= parseInt($('.cart').text());

     if((data.indexOf("2")) >= 0){
         console.log('done'); 
     }
     else {
     a++;
     $('.cart').text(a);
     $(this).addClass('activex');
     }
    }

    });

        console.log(a); 
    });
      }

});

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