在点击添加按钮的行下方,使用jQuery在表格中添加一行。

huangapple go评论115阅读模式
英文:

Add a row in a table below the row in which the add button click using Jquery

问题

我有一个表格,每一行下面都有一个“添加”按钮。我希望当我点击添加按钮时,该行将被添加到我点击“添加”按钮的特定行下面。

<table id="table_id">
  <tr>
    <th>名</th>
    <th>姓</th>
    <th>分数</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
    <th><button class="add">添加</button>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
    <th><button class="add">添加</button>
  </tr>
  <tr>
    <td>Adam</td>
    <td>Johnson</td>
    <td>67</td>
    <th><button class="add">添加</button>
  </tr>
</table>
</body>
</html>

我使用了以下JS代码。使用索引,但现在不按预期工作。

$(document).ready(function () {
    $(".add").click(function () {
        var row_index = $(this).closest("tr").index();
        var html = "<td>Adam</td><td>'Puja'</td><td>30</td><td><button class='add'>添加</button></td>";
        $("#table_id > tbody > tr").eq(row_index + 1).after(html);
    });
});

任何帮助都将不胜感激。

英文:

I have a table below which having the the 'Add' button in every row. I want when I click on the add button the row should be added below that specific row where I click ADD button.

&lt;table id=&quot;table_id&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;First Name&lt;/th&gt;
    &lt;th&gt;Last Name&lt;/th&gt;
    &lt;th&gt;Points&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Jill&lt;/td&gt;
    &lt;td&gt;Smith&lt;/td&gt;
    &lt;td&gt;50&lt;/td&gt;
    &lt;th&gt;&lt;button class=&quot;add&quot;&gt;Add&lt;/button&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Eve&lt;/td&gt;
    &lt;td&gt;Jackson&lt;/td&gt;
    &lt;td&gt;94&lt;/td&gt;
    &lt;th&gt;&lt;button class=&quot;add&quot;&gt;Add&lt;/button&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Adam&lt;/td&gt;
    &lt;td&gt;Johnson&lt;/td&gt;
    &lt;td&gt;67&lt;/td&gt;
    &lt;th&gt;&lt;button class=&quot;add&quot;&gt;Add&lt;/button&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

I have used the below JS. Using index but now working as expected.

$(document).ready(function () {
    $(&quot;.add&quot;).click(function () {
        var row_index = $(this).closest(&quot;tr&quot;).index();
        html = &quot;&lt;td&gt;Adam&lt;/td&gt;&lt;td&gt;&#39;Puja&#39;&lt;/td&gt;&lt;td&gt;30&lt;/td&gt;&lt;td&gt;&lt;button 
        class=&#39;add&#39;&gt;Add&lt;/button&gt;&lt;/td&gt;&quot;;
        (&quot;#table_id &gt; tbody &gt; tr&quot;).eq(row_index+1).after(html);
    });
});    

Any help is appreciated.

答案1

得分: 1

function Addrow(e){
var row_index = $(e).closest("tr").index()-1;
html = "

Adam 'Puja' 30

";
$('#table_id > tbody > tr').eq(row_index+1).after(html);
}

First Name Last Name Points
Jill Smith 50
Eve Jackson 94
Adam Johnson 67


英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function Addrow(e){
     var row_index = $(e).closest(&quot;tr&quot;).index()-1;
     html = &quot;&lt;tr&gt;&lt;td&gt;Adam&lt;/td&gt;&lt;td&gt;&#39;Puja&#39;&lt;/td&gt;&lt;td&gt;30&lt;/td&gt;&lt;td&gt;&lt;button class=&#39;add&#39;&gt;Add&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;&quot;;
     $(&#39;#table_id  &gt; tbody &gt; tr&#39;).eq(row_index+1).after(html);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table id=&quot;table_id&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;First Name&lt;/th&gt;
    &lt;th&gt;Last Name&lt;/th&gt;
    &lt;th&gt;Points&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Jill&lt;/td&gt;
    &lt;td&gt;Smith&lt;/td&gt;
    &lt;td&gt;50&lt;/td&gt;
    &lt;th&gt;&lt;button class=&quot;add&quot; onclick=&quot;Addrow(this)&quot;&gt;Add&lt;/button&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Eve&lt;/td&gt;
    &lt;td&gt;Jackson&lt;/td&gt;
    &lt;td&gt;94&lt;/td&gt;
    &lt;th&gt;&lt;button class=&quot;add&quot; onclick=&quot;Addrow(this)&quot;&gt;Add&lt;/button&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Adam&lt;/td&gt;
    &lt;td&gt;Johnson&lt;/td&gt;
    &lt;td&gt;67&lt;/td&gt;
    &lt;th&gt;&lt;button class=&quot;add&quot; onclick=&quot;Addrow(this)&quot;&gt;Add&lt;/button&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Try this code.

huangapple
  • 本文由 发表于 2023年3月9日 19:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定