我有一个带有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。但它没有这样做。
有没有人有想法如何在格子里写入 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");
APP.payload.user_id
和data.player1
的值吗?显然,它们不同于逻辑流程。你需要确定原因。鉴于代码示例有限,我们无法真正提供帮助。 - Rory McCrossanAPP.payload.user_id
和data.player1
给我相同的 ID 返回... 这个完美地运作。问题在于下面这行代码,我认为它没有按照我的预期工作。 - Aalberts_M.L.data.field
是什么。 - Rory McCrossandata.field
gives me my HTML ID's. I've wrote them inside thegameController.putScore('X.X')
.After this I used them inside my backend php to create my2 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中的td的ID也是1.0
。那么这一行代码看起来应该是这样的:$("#1.0").text("X")
据我所记,这应该可以工作。但实际上并没有。 - Aalberts_M.L.