这里是玩 2 人 TIC-TAC-TOE 的 JS 代码的一部分
<script type="text/javascript">
//Global Variables
var painted;
var content;
var winningCombinations;
var turn = 0;
var theCanvas;
var c;
var cxt;
var squaresFilled = 0;
var w;
var y;
//Instanciate Arrays
window.onload=function(){
painted = new Array();
content = new Array();
winningCombinations = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];
for(var l = 0; l <= 8; l++){
painted[l] = false;
content[l]='';
}
}
//Game methods
function canvasClicked(canvasNumber){
theCanvas = "canvas"+canvasNumber;
c = document.getElementById(theCanvas);
cxt = c.getContext("2d");
if(painted[canvasNumber-1] ==false){
if(turn%2==0){
/* Draw X */
cxt.beginPath();
cxt.moveTo(10,10);
cxt.lineTo(40,40);
cxt.moveTo(40,10);
cxt.lineTo(10,40);
cxt.stroke();
cxt.closePath();
content[canvasNumber-1] = 'X';
}
else{
/* Draw circle */
cxt.beginPath();
cxt.arc(25,25,20,0,Math.PI*2,true);
cxt.stroke();
cxt.closePath();
content[canvasNumber-1] = 'O';
}
turn = turn + 1;
painted[canvasNumber-1] = true;
squaresFilled++;
checkForWinners(content[canvasNumber-1]);
if(squaresFilled==9){
alert("Game Over");
location.reload(true);
}
}
我想从单选按钮中选择一个值,即:
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="radio" name="players" id="playerx"/>
<label for="playerx"><b>TEAM - X</b></label>
<input type="radio" name="players" id="playery"/>
<label for="playery"><b>TEAM - O</b></label>
</fieldset>
假设如果玩家点击TEAM - X
我希望全局变量加 1,否则加 2。
我无法从单选按钮中正确选择值或 ID 来解决此问题。有人可以帮忙吗?