简体   繁体   English

Jquery addClass() 、 removeClass() 函数不起作用

[英]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.每当单击带有类推送的按钮但它不起作用并且代码中没有错误并且它没有删除类时,我试图删除类 activex。 If i remove the class manually in the chrome console with class .push it works.如果我在 Chrome 控制台中使用 class .push 手动删除该类,它会起作用。

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.每当单击带有类推送的按钮但它不起作用并且代码中没有错误并且它没有删除类时,我试图删除类 activex。 If i remove the class manually in the chrome console with class .push it works.如果我在 Chrome 控制台中使用 class .push 手动删除该类,它会起作用。

Here is your updated code, just i have removed children() from your code.这是您更新的代码,只是我从您的代码中删除了 children()。 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); 
    });
      }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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