我试图通过按按钮来增加或减少变量的值。我现在的代码情况如何,变量保持不变。
// Variables
var deck = []
discNum = 3,
$board = $('.board'),
$tower = $board.find('.tower');
// Increase/Decrease discNum (not working?)
$('.ad').click(function() {
discNum++
})
$('.sub').click(function() {
discNum--
})
console.log(discNum)
<button class="ad">+</button>
<button class="sub">-</button>
discNum保持不变,除非我手动更改它。
答案 0 :(得分:0)
尝试一下:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<button class="ad">+</button>
<button class="sub">-</button>
</body>
</html>
<script>
var discNum = 3;
$( ".ad" ).click(function() {
discNum = discNum + 1;
alert( "New value of discNum: "+discNum );
});
$( ".sub" ).click(function() {
discNum = discNum - 1;
alert( "New value of discNum: "+discNum );
});
</script>