2017-04-22 30 views
1

标签取代这是我的jQuery代码如何删除按钮,并通过jQuery的

<script> 
$(".simpleCart_shelfItem button").click(function() 
    { 
     $(this).prop('disabled', true); 
     $(this).addClass('disabled'); 
     $(this).html("Adding &nbsp;&nbsp;<i class='icon-spinner9 spin'></i>"); 

     var action = "add"; 
     var queryString = "action="+action+"&pid="+this.value; 
     var $this = $(this); 

     $.ajax({ 
       url: "add_cart", 
       data: queryString, 
       type: "POST", 
       success:function(data) 
        { 
         $this.remove(); 
         $($this).before("<a href='cart' class='btn view-more'>In Your Cart (View)</a>"); 
        }, 
       error:function(){} 
      }); 
    }); 
</script> 

这是我的HTML代码:

<div class="simpleCart_shelfItem"> 
    <p><span>&#8377; price</span> <i class="item_price">&#8377; selling price</i></p> 
    <button value="some value" class="btn view-more">Add To Cart</button> 
    <a href="product?id=id> 
     <button class="hashs-cart">Buy Now</button> 
    </a> 
</div> 

我想要实现的是尽快用户点击添加到购物车的按钮应该被删除,并用此替换=><a href="cart" class="btn view-more">In Your Cart (View)</a>

通过使用上述JQuery代码我能够删除按钮,但无法repl与<a>标签一起使用。

任何人都可以帮助这个,为什么会这样呢?

+0

aaaaand如果我从购物车中删除吗? :D –

+1

'$ this'已经**了** $(this)'所以不需要'$($ this)' - 只需使用'$ this'。 *这个*清楚吗? –

+0

如果你删除$ this.remove()的意思。那么你不能使用$(this)来使用$(this)添加元素。因为它没有更多 – JYoThI

回答

0
$($this).before 

应该$this.replacewith

<script> 
$(".simpleCart_shelfItem button").click(function() 
{ 
    $(this).prop('disabled', true); 
    $(this).addClass('disabled'); 
    $(this).html("Adding &nbsp;&nbsp;<i class='icon-spinner9 spin'></i>"); 

    var action = "add"; 
    var queryString = "action="+action+"&pid="+this.value; 
    var $this = $(this); 

    $.ajax({ 
      url: "add_cart", 
      data: queryString, 
      type: "POST", 
      success:function(data) 
       { 
        /*$this.remove();*/ //remove this line 
        $this.replacewith("<a href='cart' class='btn view-more'>In Your Cart (View)</a>"); 
       }, 
      error:function(){} 
     }); 
});