dongyi3616 2016-10-18 10:50
浏览 39

获取ajax时点击时jquery更新项目计数

I have created a ajax load more button to grab more data from my database, which all works fine. I am now trying to do a count, so the button displays how many results are left to display. Here is my jQuery code that is fired when the button is clicked:

jQuery(document).ready(function(){
    jQuery(document).on('click','.show_more',function(){
        var ID = jQuery(this).attr('id');
        var count = jQuery(".singleproduct").length;
        var totals = jQuery('.totalleft').text();
        var finaltotal = totals - count;
        //jQuery('.show_more').hide();
        jQuery('.loding').show();
        jQuery.ajax({
            type:'POST',
            async: true,
            crossDomain : true,
            url:'example.com/data.php',
            data:'count='+count,
            success:function(html){
            //jQuery('#show_more_main'+ID).remove();
              jQuery('.retailitems').append(html);
              jQuery('html, body').animate({scrollTop: jQuery(".site-info").offset().top}, 1000);

            }
        }); 
        jQuery( ".totalleft" ).replaceWith( finaltotal );           
    });
});

And here is the code for the button:

<span id="<?php echo $result['id']; ?>" class="show_more" title="Load more posts">Load <span class="totalleft"><?php echo $loadmore;?></span> more</span>

When clicked for the first time, the button updates with the correct remaining results. When clicked a second time, the news results populate, but the count remains the same.

Do i have things in the right order above?

Thanks

EDIT

So i found the issue. The calculations were correct, but when updating the count on the button i was actually removing the class of 'totalleft' with this:

jQuery( ".totalleft" ).replaceWith( finaltotal );

So i replaced this with:

jQuery( ".totalleft" ).text( finaltotal );

and all is fine, thanks for all the help.

  • 写回答

1条回答 默认 最新

  • dongyi8795 2016-10-18 10:53
    关注

    parse the text() in .totalleft to get an integer value

    var totals = parseInt(jQuery('.totalleft').text());
    

    if your calculations are correct, you should get the right number back

    EDIT

    because ajax is async you are re-updating the value of .totalleft before the ajax is complete.

    make sure that you are doing that in the ajax callback:

    success:function(html){
       //jQuery('#show_more_main'+ID).remove();
       jQuery('.retailitems').append(html);
       jQuery('html, body').animate({scrollTop: jQuery(".site-info").offset().top}, 1000);
    
       jQuery( ".totalleft" ).replaceWith( finaltotal );
    }
    

    finally, I'm not sure what var count = jQuery(".singleproduct").length; is so make sure that the values you are expecting are correct by console logging before and after the subtraction

    EDIT #2

    var count = jQuery(`.singleproduct`).length;
    console.log('count :' + count);
    var totals = jQuery('.totalleft').text();
    console.log('totals :' + totals);
    

    are you getting numbers on the above logs? if not, then you might be replacing some part of your html with the ajax request and singleproduct or totalleft may not be there anymore

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line