单击按钮不会增加或减少变量

时间:2019-01-09 18:42:43

标签: jquery

我试图通过按按钮来增加或减少变量的值。我现在的代码情况如何,变量保持不变。

jQuery

    // 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)

HTML

    <button class="ad">+</button>
    <button class="sub">-</button>

discNum保持不变,除非我手动更改它。

1 个答案:

答案 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>