-2

我有一个包含 9 个小板的板,每个小板包含 9 个单元(终极井字游戏)。我正在尝试使用单击功能并在单击的按钮上打印“x”,但它不会更改按钮的文本,我不知道为什么。请帮我。这是代码:

    <script>
        var bigTable = "<table align='center' value='bigBoard' border='0' cellpadding='1' cellspacing='1'\><tbody>";
        for (var k = 0; k < 9; k++) 
        {
            if (k % 3 === 0)
            {
                bigTable += "<tr>";
            }

            bigTable += "<td>";
            var mytable = "<table value='smallBoard'+k border='1' cellpadding='1' cellspacing='1'><tbody><tr>";
            for (var j = 0; j < 3; j++)
            {
                for (var i = 0; i < 3; i++)
                {
                    var position = +k + "," + j + "," + i;
                    mytable += "<td><button class='a' id=" + position + " type='submit' name=" + position + "></button</td>";
                }

                mytable += "</tr><tr>";
            }

            mytable += "</tr></tbody></table>";
            bigTable += mytable + "</td>";

            if (k % 3 === 2)
            {
                bigTable += "</tr>";
            }
        }

        bigTable += "</tbody></table>";
        document.write(bigTable);
    </script>

    <script>
        $(document).ready(function() {
            $(".a").click(function() {
            var buttonPressed = $(this).attr("id");
                jQuery.ajax({
                        data:buttonPressed,
                        url:"board",
                        success: function(responseText){
                            //alert("acac" + buttonPressed);
                            $("#" + buttonPressed).text(responseText);

                        }
                   }); 
                });
            });
    </script>
4

3 回答 3

0

将按钮的类型提交更改为按钮,因为您正在调用 Ajax 请求,而不是提交表单。

于 2013-09-23T12:24:54.430 回答
0

我运行了这个 jsfiddle,它工作正常

http://jsfiddle.net/P8wrb/1/

只需为单击的按钮声明一个变量,然后通过引用为其设置 .text

$(".a").click(function() {
     var that = $(this);
     ....
     that.text('X'); 
}
于 2013-09-23T12:29:19.573 回答
0

这是你做错了,改变这个:

变量位置 = +k + "," + j + "," + i;

到类似的东西,

变量位置=+k+“-”+j+“-”+i;

于 2013-09-23T12:32:31.960 回答