簡體   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