【问题标题】:Why aren't the variables being updated when I trigger an event?为什么触发事件时变量没有更新?
【发布时间】:2017-02-15 05:00:19
【问题描述】:

我对编程很陌生,所以很高兴能得到一些帮助。

为什么当我按下按钮时变量没有更新??

html:

    <h1>NIM</h1>
    <p>Welcome to a simple edition of the game NIM</p>
    <table>
        <tr>
            <th>You</th>
            <th>Left</th>
            <th>PC</th>
        </tr>
        <tr>
            <td><p id="pl" class="nr">0</p></td>
            <td><p id="a" class="nr">25</p></td>
            <td><p id="pc" class="nr">0</p></td>
        </tr>
    </table>

        <br>
    <p>How many do you want to pull?</p>
    <input type="number" id="val" value="1" min="1" max="3">
    <button id="turn">Trekk</button>

JavaScript:

$('#turn').click(function () {
var val    = parseInt($('#val').val()),
    player = parseInt($('#pl').html()),
    pc     = parseInt($('#pc').html()),
    total  = parseInt($('#a').html());
console.log(val);
console.log(total);

do {
    switch (val) {
    case 1:
        total -= 4;
        player += 1;
        pc += 3;
        break;
    case 'b2':
        total -= 4;
        player += 2;
        pc += 2;
        break;
    case 'b3':
        total -= 4;
        player += 3;
        pc += 1;
        break;
    }

} while (total > 1);});

我可能更熟悉 c++,这是我第一次尝试在 javascript/jquery 中使用 do 循环。

【问题讨论】:

  • 那段代码执行了吗?
  • 我在这里创建了fiddle,变量正在更新中。文本没有更新,但那是因为你没有在任何地方更新它
  • 您的代码正在运行。变量改变。在do 循环之后使用console.log()
  • 你会接受答案还是做出反应?

标签: javascript jquery loops switch-statement var


【解决方案1】:

我认为这就是你想要制作的游戏。好好享受!

<html> 

<head> 

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 

<script>
$(document).ready(function(){

    var val    = 0; //Initial value of the input box 
    var player = parseInt($('#pl').html()); //Player score
    var pc     = parseInt($('#pc').html()); //pc score
    var total  = parseInt($('#a').html()); //Total left

    $('#turn').click(function () {

        val = parseInt($('#val').val());

        console.log(val);
        console.log(total);

        if(total > 0){

            switch (val) {
            case 1:
                total -= 4;
                player += 1;
                pc += 3;
                break;
            case 2:
                total -= 4;
                player += 2;
                pc += 2;
                break;
            case 3:
                total -= 4;
                player += 3;
                pc += 1;
                break;
            }

            $('#pl').html(player);
            $('#pc').html(pc);
            $('#a').html(total);
        }
    });

});

</script>

</head> 

<body> 

    <h1>NIM</h1>
    <p>Welcome to a simple edition of the game NIM</p>
    <table>
        <tr>
            <th>You</th>
            <th>Left</th>
            <th>PC</th>
        </tr>
        <tr>
            <td><p id="pl">0</p></td>
            <td><p id="a">25</p></td>
            <td><p id="pc">0</p></td>
        </tr>
    </table>

        <br>
    <p>How many do you want to pull?</p>
    <input type="number" id="val" value="1" min="1" max="3">
    <button id="turn">Trekk</button>

</body> 

</html> 

小提琴

https://jsfiddle.net/1o6qxw9c/

你的错误

  1. 你不能像这样直接使用'$'。包括 jQuery。
  2. val 的值由 '0' 初始化。
  3. 您只需要更新 val 并根据第一次获取值计算其余的东西。如果您在单击按钮时编写所有代码 - 所有其他值也将被重置。
  4. 将所有代码包装在 document.ready() 中,以便它们正常工作。
  5. 您需要更新总、个人计算机和玩家得分的值,一旦计算出您错过的值。

JavaScript 不同于“C”。恭喜您使用 Web 语言,因为 JS 在客户端和服务器端都使用。努力吧,先学简单的基本操作,再过一年了解OOJS。虽然,它提供了出色的功能,但如果没有很好地理解,那么大多数时候你会对弹出的结果感到困惑!

【讨论】:

    猜你喜欢
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2020-04-15
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多