如何通过使用onClick()函数单击按钮来填充Javascript中的表

How do I populate a table in Javascript by clicking a button using the onClick() function?

本文关键字:填充 按钮 Javascript 单击 函数 何通过 onClick      更新时间:2023-09-26

使用JavaScript,我想填充soduku.setstring()中的数字;一旦用户点击"Easy"按钮,就可以将其放入表格中。我如何使用onClick()函数实现这一点?

function initialize() {
var col, row;
for (row = 0; row <=8; row++) {
for (col=0; col <= 8; col++) {
var cell = document.getElementById('cell_' + col + '_' + row);
if (!parseInt(cell.innerHTML)) {
// cell is empty
cell.onclick = selectCell;
cell.className = 'tofill';
}
}
}
document.onkeypress = keyPress;
}
function easygame(){
    var sudoku.setString("240057360000008009378000200060041702080000005000005010130000000050003026000812000");
    loadPuzzle(sudoku);
    updateUI();
}
function updateUI(){
    initialize();
}

<input type="button" value="Easy" onclick="easygame()"/>
<table align=center id="sudoku" cellspacing=0>
<tr>
<td id="cell_0_0"></td>
<td id="cell_1_0"></td>
<td id="cell_2_0"></td>
<td id="cell_3_0"></td>
<td id="cell_4_0"></td>
<td id="cell_5_0"></td>
<td id="cell_6_0"></td>
<td id="cell_7_0"></td>
<td id="cell_8_0"></td>
<tr>

对于数独,为什么不使用2D数组呢?它在逻辑上比使用字符串更容易。就您的问题而言,您可以使用jQuery像这样填充表格,

for(var i=0;i<81;i++)
{
    $('table#sudoku td:eq('+i+')').text(sudokuString[i]);   
}