2017-12-29 5 views
2
$('.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); 
    }); 
    } 
}); 

작동하지 않습니다 클래스. 클래스 .push가있는 크롬 콘솔에서 클래스를 수동으로 제거하면 작동합니다.JQuery와 addClass(), removeClass() 함수는 클래스 푸시 버튼이 클릭 될 때마다 나는 클래스 액티브을 제거하기 위해 노력하고 있지만이 작동하지 및 오류 코드에 없다 그것을 제거 아니에요

클래스 푸시 버튼이 클릭 되었으나 작동하지 않고 코드에 오류가없고 클래스를 제거하지 않을 때마다 클래스 activex를 제거하려고합니다. 클래스 .push가있는 크롬 콘솔에서 클래스를 수동으로 제거하면 작동합니다.

+0

'$ (이) .children()는 removeClass ('액티브'). HTML을 공유하세요 '등의 –

+0

에 대한 동일,''$ (이) .removeClass ('액티브 ') 할 필요가 – brk

+0

버튼에 대한 Heres html 페이지에 많은 버튼이 있습니다 –

답변

0

업데이트 된 코드는 다음과 같습니다. 코드에서 children()을 제거했습니다. 도움이되는지 아닌지 알려주세요.

$('.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); 
    }); 
     } 

}); 
+0

Sanjay 감사합니다. 그러나 코드가 작동하지 않습니다. 해당 요소의 하위 클래스를 변경해야합니다. –

+0

@krishnasapkal에 오신 것을 환영합니다. 그렇다면 귀하의 html을 공유하는 것에 관한 질문이 있으시면 귀하를 도와 드리겠습니다. –

+0

heres html code

관련 문제