【问题标题】:How to replace data in a specific row jQuery如何替换特定行中的数据jQuery
【发布时间】:2018-04-30 03:31:37
【问题描述】:

大家好,我想根据行号替换行内的数据。我发布的代码遍历表中的行,计算行索引并将它们与所需的行匹配。如果条件为真,则应替换该行中的数据。 我已经编辑了问题

我不知道我应该用什么来替换数据?

我尝试过 .innerHTML、.replaceWith() 但它们没有给我所需的东西。

$('table#table-mytable tr').each(function(){
  var row_required = tag[tag.length-2];
  console.log('this is row_required: '+row_required);
  var row = $(this).index()+1;
  console.log('this is row: '+row);
  if(row==row_required) {
     (What should I enter here)
  }
});

我想替换为: replaceWith('td colspan="2" input type="text" name="input_" class="input_" id="input_" value="' + input_tag + '" placeholder="输入标签" /td>') ?

【问题讨论】:

  • 您正在循环行。然而,一行没有内容。它有包含内容的单元格 (<td>)。所以你正在寻找类似row.find('td').html('The new text')

标签: jquery


【解决方案1】:

您可以使用html()(以及其他方法)替换tr 的内容。另请注意,您根本不需要循环。如果您知道索引,您可以直接使用eq() 选择它:

var row_required = 1;
$('#table-mytable tr').eq(row_required).html('<td>Bar</td>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table id="table-mytable">
  <tbody>
    <tr><td>Foo</td></tr>
    <tr><td>Foo</td></tr>
    <tr><td>Foo</td></tr>
  </tbody>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2021-01-05
    • 1970-01-01
    相关资源
    最近更新 更多