我正在尝试更改我的动态表格中特定行中的一个特定值。我的问题是,由于我的表的动态状态,总是只更改第一行。
下面是一个示例:
var tds = '<tr id="tr">';
tds += '<td><input type="text" id="id1" value="" size="3"></td>';
tds += '<td id="id2"></td>';
tds += '<td id="id3"></td>';
tds += '</tr>';
$("#table").append(tds);例如,在这里,我想更改表的第2行或第二次迭代中的值。有人能给出个主意吗?谢谢
发布于 2015-08-27 16:48:48
似乎您对多个元素使用了相同的ID名称,那么它将只为第一个匹配的元素返回。在这种情况下,我不知道您想要侦听click或其他什么事件,如果您已经知道要编辑哪一行,下面是示例解决方案:
使用选择器
// will select second row
// and find the input
// then change it value into "HELLOO"
// nth-child start with 1(index)
// 1 = 1st row
// 2 = 2nd row
$('#table tr:nth-child(2)').find('input').val('HELLOO');使用函数
// will select second row
// and find the input
// then change it value into "HELLOO"
// eq is a zero-based index
// 0 = 1st row
// 1 = 2nd row
$('#table tr').eq(1).find('input').val('HELLOO');发布于 2015-08-27 16:49:07
首先,您的示例只有一行。如果你打算插入到第二列,你应该这样做:
$("#table>tr>td:nth-child(2)").html('something you want');或
$("#table>tr>td:nth-child(2)").text('something you want');如果您真的想要第二行,请执行以下操作:
var html ='<tr>' ;
html += '<td><input type="text" id="id2" value="" size="3"></td>';
html += '<td id="id2"></td>';
html += '<td id="id3"></td>';
html += '</tr>';
$( html ).insertAfter( $("#table>tr:nth-child(1)" ) ); 发布于 2015-08-27 17:08:56
你可以试试
var tds = '<td><input type="text" id="id1" value="" size="3"</td>'; tds += '<td id="id2"></td>'; tds += '<td id="id3"></td>'; $('#table tr:nth-child(2)').html(tds);
这将替换完整的第二行
https://stackoverflow.com/questions/32244498
复制相似问题