使用jQuery和JavaScript中的if语句编写HTML文件

5

我有一个带有Tic Tac Toe游戏区的HTML。其中包含多个onclick

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。但它没有这样做。
有没有人有想法如何在格子里写入 XputScore 函数 (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");


4
我该如何调试我的JavaScript代码? - Andreas
你检查了APP.payload.user_iddata.player1的值吗?显然,它们不同于逻辑流程。你需要确定原因。鉴于代码示例有限,我们无法真正提供帮助。 - Rory McCrossan
APP.payload.user_iddata.player1 给我相同的 ID 返回... 这个完美地运作。问题在于下面这行代码,我认为它没有按照我的预期工作。 - Aalberts_M.L.
好的,那么请检查一下 data.field 是什么。 - Rory McCrossan
data.fieldgives me my HTML ID's. I've wrote them inside the gameController.putScore('X.X').After this I used them inside my backend php to create my 2 dimensional array.but I am receiving the same strings as in the clinches right behind thegameController.putScore - Aalberts_M.L.
当我点击gameController.putScore('1.0')时,我的HTML中的tdID也是1.0。那么这一行代码看起来应该是这样的:$("#1.0").text("X")据我所记,这应该可以工作。但实际上并没有。 - Aalberts_M.L.
1个回答

4

你为什么不直接使用选项,而要使用jquery使其变得复杂?

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

这里需要做些什么呢?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接