douqu2481 2014-06-25 03:40
浏览 31
已采纳

五位数计数器

I am struck how to start counter with five digit counter. on button click. A request will go to server via ajax to store current value,so every time a new user will come if he press the button the counter will increase by 1.

    00001
    00002
    .
    00010
    .
    .
    99999

        $("#update").click(function() {
            $('#counter').html(function(i, val) {
                /*$.ajax({
                    url: '/path/to/script/',
                    type: 'POST',
                    data: {increment: true},
                    success: function() { alert('Request has returned') }
                });*/
                return + val+1;
            });
        });

<button id="update" type="button">Click Me</button>
<div id="counter">00000</div>
  • 写回答

3条回答 默认 最新

  • douduiti3040 2014-06-25 03:43
    关注

    You need to parse into int, increase and then parse to a string and to add it the "00000", then sub the unneeded zeroes.

     function increment(num) {
        var newNumber = parseInt(num) + 1
        var str = "00000" + newNumber;
        return str.substr(str.length-5);
    }
    
    
    
       $("#update").click(function() {
                $('#counter').html(function(i, val) {
                    /*$.ajax({
                        url: '/path/to/script/',
                        type: 'POST',
                        data: {increment: true},
                        success: function() { alert('Request has returned') }
                    });*/
                    return increment(val)
                });
            });
    
    
    
    
    
    <button id="update" type="button">Click Me</button>
    <div id="counter">00000</div>
    

    fiddle - http://fiddle.jshell.net/L3mRD/2/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部