Javascript 如何按行和列索引设置单元格的值?

Javascript 如何按行和列索引设置单元格的值?,javascript,jquery,Javascript,Jquery,到目前为止,我有这个,但它不会改变单元格值: function setCellValue(tableId, rowId, colNum, newValue) { $('#'+tableId).find('tr#'+rowId).find('td:eq(colNum)').html(newValue); }; 通过连接索引创建选择器(在中,索引从0开始)。尽管您需要对行选择器执行相同的操作,因为rowId是tr的索引,而不是id function setCellValue(tableId

到目前为止,我有这个,但它不会改变单元格值:

function setCellValue(tableId, rowId, colNum, newValue)
{
    $('#'+tableId).find('tr#'+rowId).find('td:eq(colNum)').html(newValue);
};

通过连接索引创建选择器(在中,索引从
0
开始)。尽管您需要对行选择器执行相同的操作,因为
rowId
tr
的索引,而不是
id

function setCellValue(tableId, rowId, colNum, newValue)
{
    $('#'+tableId).find('tr:eq(' + (rowId - 1) + ')').find('td:eq(' + (colNum - 1) + ')').html(newValue);
};

或者使用伪类选择器

function setCellValue(tableId, rowId, colNum, newValue)
{
    $('#'+tableId).find('tr:nth-child(' + rowId + ')').find('td:nth-child(' + colNum + ')').html(newValue);
};

或通过避免使用方法使用单个选择器

function setCellValue(tableId, rowId, colNum, newValue)
{
    $('#' + tableId + ' tr:nth-child(' + rowId + ') td:nth-child(' + colNum + ')').html(newValue);
    // or
    $('#' + tableId + ' tr:eq(' + (rowId - 1) + ') td:eq(' + (colNum - 1) + ')').html(newValue);
};

你确定你的HTML已经设置了所有合适的
id
吗?旁注-既然
id
s必须是唯一的,你不应该有多个具有相同id的tr,所以你应该能够直接选择tr,而不是先选择表,然后选择tr。逻辑缺陷,为什么
$('#id')。查找('tr#id'))..
当ID是唯一的时,您只需要
$('#'+rowId+'td').eq(colNum).html(newValue)我不知道为什么,但当我尝试设置CellValue(“summaryTable”,3,3,'100')什么都没发生…@LucasSeveryn:你能在代码片段或文件中共享代码吗jsfiddle@LucasSeveryn:您在代码段中缺少jQuery库。。。。。。并使用更新的代码@LucasSeveryn:很高兴为您提供帮助:)