我想在每一行和每一列中生成随机数。我在下面这样写。它只生成两行。我想创建 4*4 数独。所以我给了这样的
<script type="text/javascript">
var random1, random2, random3, random4;
var randomArray = [0,0,0,0];
//sets array variables to random numbers
function CreateLottoValues() {
for (var i=0; i<randomArray.length; i++) {
randomArray[i] = Math.floor(Math.random()*4 + 1);
}
}
}
//create table
function UpdateTable() {
CreateLottoValues();
for (var i=0; i<randomArray.length; i++) {
tmp = 'cell'+(i+1);
document.getElementById(tmp).innerHTML = randomArray[i];
}
}
</script>
</head>
<body onload="UpdateTable();">
<center>
<div id="container">
<div id="header"> <h1>Welcome</h1> </div>
<div id="content">
<span id="tableSpan">
<table border="1" id="lotto">
<tr>
<td class="normal" id="cell1"> </td>
<td class="normal" ><input type="text" name=""></td>
<td class="normal" > </td>
<td class="red" id="cell4"> </td>
</tr>
<tr>
<td class="normal" id="cell1"> </td>
<td class="normal" id="cell2"> </td>
<td class="normal" id="cell3"> </td>
<td class="red" id="cell4"> </td>
</tr>
<tr>
<td class="normal" id="cell1"> </td>
<td class="normal" id="cell2"> </td>
<td class="normal" id="cell3"> </td>
<td class="red" id="cell4"> </td>
</tr>
<tr>
<td class="normal" id="cell1"> </td>
<td class="normal" id="cell2"> </td>
<td class="normal" id="cell3"> </td>
<td class="red" id="cell4"> </td>
</tr>
</table>
</span>
<input type="button" value="Re-generate Numbers" onclick="UpdateTable();" />
请帮助我在所有行和列中生成随机数。提前致谢。