首页 > 解决方案 > 使用 jQuery 将“1”增加到格式化数字

问题描述

我在我的 HTML 中显示了一个用 PHP 格式化的数字字段。

$totalShares = get_field('share_count');
$totalFormat = number_format( $totalShares, 0, '.', ',' );

echo 'Shared: <span class="totalShares">'.$totalFormat.'</span> times';

使用 jQuery 和 Ajax,我通过单击按钮添加 1,但它会将 HTML 中的例如 8,500 (8500) 转换为 9。如果我刷新,我有 8,501。

jQuery(function($) {
    
    $('.social-icon').on('click', function() {
        
        $.ajax({
            url: bloginfo.site_url + '/wp-json/functions/shares/' + bloginfo.post_id.ID,
            type: 'post',
            success: function() {
                console.log('works!');
            },
            error: function() {
                console.log('failed!');
            }
        });
        
        // Change the like number in the HTML to add 1
        var updated_shares = parseInt($('.totalShares').html()) + 1;
        
        $('.totalShares').html(updated_shares);
                
    });
    
});

如何纠正这部分var updated_likes = parseInt($('.share_count').html()) + 1;

谢谢您的帮助。

标签: jqueryajax

解决方案


推荐阅读