Jquery .span刷新onclick

时间:2012-03-28 14:31:23

标签: javascript jquery

我认为这应该有用吗?

我有这个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 个答案:

答案 0 :(得分: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);
        });

更新2

如果用户输入任意数量的quatity

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);
            });