繁体   English   中英

在 .html 中使用 JQuery 和 Javascript 中的 if 语句编写

[英]Writing inside a .html using JQuery and an if Statement in Javascript

我有一个带有 Tic Tac Toe 字段的 HTML。 有几个onlick

HTML (game.html):

<table class="nes-table is-bordered is-centered" id="tttpattern">
             <tbody>
             <tr>
                 <td class="square" id="0.0" onclick="gameController.putScore('0.0')"></td>
                 <td class="square" id="0.1" onclick="gameController.putScore('0.1')"></td>
                 <td class="square" id="0.2" onclick="gameController.putScore('0.2')"></td>
             </tr>
             <tr>
                 <td class="square" id="1.0" onclick="gameController.putScore('1.0')"></td>
                 <td class="square" id="1.1" onclick="gameController.putScore('1.1')"></td>
                 <td class="square" id="1.2" onclick="gameController.putScore('1.2')"></td>
             </tr>
             <tr>
                 <td class="square" id="2.0" onclick="gameController.putScore('2.0')"></td>
                 <td class="square" id="2.1" onclick="gameController.putScore('2.1')"></td>
                 <td class="square" id="2.2" onclick="gameController.putScore('2.2')"></td>
             </tr>
             </tbody>
         </table>

当我单击其中一个onclick ,我的代码会在我的gameController.js运行putScore函数。

在这个 js 中,我想检查我的payload使用的user_id是否与我从数据库中获得的player1 ID 相同。

如果是这种情况,我的程序应该在我单击的方块中写一个X。 但事实并非如此。

有人知道如何在正方形内写X吗?

putScore 函数 (gameController.js)

putScore: function (option) {
        gameModel.putScore(APP.route.param.gameID, {"field" : option}).then(function (data) {

                  if (APP.payload.user_id === data.player1) {
                      $("#" + data.field).text("X");
                  } else {
                      $("#" + data.field).text("O");
                  }
        }).catch(function(e) {APP.logError(e)});
    }

我做错了什么吗?

我认为重要的部分是: $("#" + data.field).text("X");

您是否有理由不使用本机选项而不是使用 jquery 使其复杂化?

document.getElementById(option).innerHTML = "X";

会在这里做伎俩吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM