简体   繁体   English

将输入追加到表的下一行

[英]Append input to next row of table

How can i add an input to the next row in a table, and have it be part of the form that eventually gets submitted? 如何将输入添加到表的下一行,并将其作为最终提交表单的一部分? in this case, a row after the address row. 在这种情况下,地址行之后的一行。

fiddle 小提琴

$(function(){
       $('#addRow').click(function(){
           $("<tr><td><input type='text' value='' /></td></tr>")
           .attr("id", "city")
           .attr("name", "city")
           .appendTo("#test");
       });
      $('#sub').click(function(){
         var data = $('#test').serialize();
         alert(data);
      });
});


<form id='test' name='test' method='POST' action='#'>
<table align='center'>
<tr>
  <td>
    <input type='button' id='addRow' value='Add'>
  </td>
</tr>
<tr>
  <td>Name:</td>
  <td>
    <input type='text' id='name' name='name'>
  </td>
</tr>
<tr>
  <td>Address:</td>
  <td>
    <input type='text' id='address' name='address'>
  </td>
</tr>
<tR>
  <td>
    <input type='button' id='sub'>
  </td>
</tr>

Well here, you're appending it to the form. 好了,您将其添加到表单中。 That's why the textbox is being thrown at the bottom. 这就是为什么将文本框扔到底部的原因。

If you want to insert it under the last row, what you want to do is .appendTo the last tr in your table. 如果要将其插入到最后一行下, .appendTo执行的操作是.appendTo表中的最后一个tr。 I would try doing something like: 我会尝试做类似的事情:

.appendTo("tr:last");

My assumption is that you want to add another row for a textbox under all the other text boxes. 我的假设是您想在所有其他文本框下为文本框添加另一行。 In that case, I would recommend using the .insertBefore command. 在这种情况下,我建议使用.insertBefore命令。 That way the box goes before the submit button at the bottom. 这样,该框位于底部的“提交”按钮之前。 Oh, and you'll also probably want to add a label for that textbox (another td tag in your JQuery). 哦,您可能还想为该文本框添加一个标签(JQuery中的另一个td标签)。

.insertBefore("tr:last");

See if this helps! 看看会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM