jQuery:加班加值

时间:2016-08-28 04:59:14

标签: jquery

如果我按下1按钮,无论何时我是银色或金色都会询问。

如果我点击白银,我的奖金将增加+150 000

现在我正在开展考试,我的奖金将: 增加150k,300k,450k等

但现在, 输出为0150000150000150000


这是我的代码

var currentCircleId = '';
var balance='0'
$(document).ready(function(){
        $(".circle").click(function(){
        $(".selection").toggle();
        currentCircleId = $(this).attr('id');
    });
    $('.selection').click(function(){
        $(".selection").toggle();
        var newClass = $(this).data('new-class');
        $('#' + currentCircleId).removeClass('gold silver').addClass(newClass);
    });

    $('.countsilver').click(function(){
        balance+=150000;
        $("#bonus1").html(balance);
    });
});
.circle{
	border-radius: 50%;
	border: solid 1px black;
	height: 33px;width: 33px;
	background: none;
	position: relative;
	cursor: pointer;
}
.silver{
	background-color:#c0c0c0;
}
.gold{
	background-color:#ffd700;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<a class="selection countsilver" data-new-class="silver" style="display: none;">SILVER</a>
	<a class="selection countgold" data-new-class="gold" style="display: none;">GOLD</a>
	<div class="circle" id="16"><p id="z16"></p></div>

	<p>Bonus 1 gain = <span id="bonus1"></span>

有人能告诉我正确的方法吗? 谢谢

2 个答案:

答案 0 :(得分:2)

原因是您已将余额变量声明为字符串对象。

var balance = '0';

所以它每次都会连接150000。 您必须将余额声明为

var balance = 0;

现在你的balance对象将作为一个整数,现在它将添加0 + 150000等等。

答案 1 :(得分:0)

var currentCircleId = '';
var balance=0;
$(document).ready(function(){
        $(".circle").click(function(){
        $(".selection").toggle();
        currentCircleId = $(this).attr('id');
    });
    $('.selection').click(function(){
        $(".selection").toggle();
        var newClass = $(this).data('new-class');
        $('#' + currentCircleId).removeClass('gold silver').addClass(newClass);
    });

    $('.countsilver').click(function(){
        balance+=150000;
        $("#bonus1").html(balance);
    });
});
.circle{
	border-radius: 50%;
	border: solid 1px black;
	height: 33px;width: 33px;
	background: none;
	position: relative;
	cursor: pointer;
}
.silver{
	background-color:#c0c0c0;
}
.gold{
	background-color:#ffd700;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<a class="selection countsilver" data-new-class="silver" style="display: none;">SILVER</a>
	<a class="selection countgold" data-new-class="gold" style="display: none;">GOLD</a>
	<div class="circle" id="16"><p id="z16"></p></div>

	<p>Bonus 1 gain = <span id="bonus1"></span>