2012-03-28 110 views
0

我正確地認爲這應該工作嗎?Jquery .span刷新onclick

我有這個jQuery:

<script> 
    $(function() { 
     $(".wpsc_buy_button").click(function(evt) { 
     $(".counter").load("index.php") 
     evt.preventDefault(); 
     }) 
    }) 
</script> 

這.span:

<span class="counter">    
    <?php 
     $i = 0; 
     while(wpsc_have_cart_items()): wpsc_the_cart_item(); 
    ?> 
    <?php $i += wpsc_cart_item_quantity(); ?> 
    <?php endwhile; ?> 
    <?php print $i ?> 
</span> 

,這個按鈕:

<input type="submit" value="Add To Cart" name="Buy" class="wpsc_buy_button" id="product_16_submit_button"/> 
+1

是,將工作 - 我想補充一個';'的'load'線完整性......結束時,如果它不工作,什麼是你期待它要做什麼? – ManseUK 2012-03-28 14:34:02

+0

我想在點擊提交按鈕時刷新span.counter。雖然似乎不適合我。 – bskool 2012-03-28 14:37:32

+0

你的代碼會覆蓋'counter'的內容,看起來好像你向服務器發送任何東西..看看'get()'或'post()' – ManseUK 2012-03-28 14:38:32

回答

0

您可以更改代碼以這種方式工作:

<script> 
    $(function() { 
     $(".wpsc_buy_button").click(function(evt) { 
     $.get("index.php", function(){ 
      var current = parseInt($.trim($(".counter").text())); 
      $(".counter").text(current+1); 
     }); 
     evt.preventDefault(); 
     }) 
    }) 
</script> 

已更新

您正在努力做到這一點。這裏是你應該做的方式: 找到文件http://tobyclothing.com/wp-content/plugins/wp-e-commerce/wpsc-core/js/wp-e-commerce.js?ver=3.8.7.6.2.494864,發現這些行(行248 - 257)

  jQuery.post('index.php?ajax=true', form_values, function(returned_data) { 
       eval(returned_data); 
       jQuery('div.wpsc_loading_animation').css('visibility', 'hidden'); 

       if(jQuery('#fancy_notification') != null) { 
        jQuery('#loading_animation').css("display", 'none'); 
       //jQuery('#fancy_notificationimage').css("display", 'none'); 
       } 
    // add your NEW_CODE here 
      }); 

你NEW_CODE將是:

var current = parseInt($.trim($(".counter").text())); 
    $(".counter").text(current+1); 

那麼這將導致:

 jQuery.post('index.php?ajax=true', form_values, function(returned_data) { 
      eval(returned_data); 
      jQuery('div.wpsc_loading_animation').css('visibility', 'hidden'); 

      if(jQuery('#fancy_notification') != null) { 
       jQuery('#loading_animation').css("display", 'none'); 
      //jQuery('#fancy_notificationimage').css("display", 'none'); 
      } 
var current = parseInt($.trim($(".counter").text())); 
$(".counter").text(current+1); 
     }); 

UPDATE 2

如果用戶輸入的任何數量的生化的的

var qty = parseInt(jQuery("input[name='wpsc_quantity_update']").val()); 
      jQuery.post('index.php?ajax=true', form_values, function(returned_data) { 
       eval(returned_data); 
       jQuery('div.wpsc_loading_animation').css('visibility', 'hidden'); 

       if(jQuery('#fancy_notification') != null) { 
        jQuery('#loading_animation').css("display", 'none'); 
       //jQuery('#fancy_notificationimage').css("display", 'none'); 
       } 
    var current = parseInt(jQuery.trim(jQuery(".counter").text())); 
    jQuery(".counter").text(current+qty); 
      }); 
+0

跟着你發佈的URL,我深入瞭解代碼,並找到了如何做到這一點。請檢查更新後的答案。問候。 – safrazik 2012-03-28 16:39:35

+0

再次感謝,但仍然沒有做任何事情。 – bskool 2012-03-28 16:49:28

+0

你可以用''jQuery''代替''$''並嘗試嗎? - 'var current = parseInt(jQuery.trim(jQuery(「。counter」)。text())); jQuery(「。counter」).text(current + 1);' – safrazik 2012-03-28 16:54:45