在div内得到一个数字并计数加一 [英] get a number inside a div and count plus one

查看:255
本文介绍了在div内得到一个数字并计数加一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来获取div内的值(数字)并加1,但是如果值是1,并且我按我的按钮,它会给我01,并且我想加1,如果它是2并再次按下我的按钮,它会给我22.

I am using the code below to get the value(number) inside a div and plus one but if the value is 1 and I am pressing my button it gives me 01 and I want to plus one and if it is 2 and press my button again it gives me 22.

我的代码是:

<div class="mybtn">0</div>

<script>

$(function() {

$(".buttonDown").click(function() 
{
var checkDown = $('.buttonDown').text();
$('.mybtn').hide("fast");
$('.mybtn').text(checkDown+1);
$('.mybtn').fadeIn("slow");
});

});
</script>

推荐答案

$(function () {
    $(".mybtn").click(function () {
        var $this = $(this).stop(true, true).hide("fast");
        var checkDown = parseInt($.trim($(this).text()), 10);
        $this.text(checkDown + 1).fadeIn("slow");
    });
});

演示:小提琴

这篇关于在div内得到一个数字并计数加一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆