简体   繁体   English

动态添加/编辑表格HTML表格行

[英]Dynamically Add/Edit Table HTML Table Rows

This is driving me nuts, I've been trying all sorts of Javascripts and codes all day and can not get this to work, so hoping someone here can point me in the right direction please. 这真让我发疯,我整天都在尝试各种Javascript和代码,但无法正常工作,因此希望这里的人可以为我指明正确的方向。

Here is my current table, and I want to create an "advanced" option that allows for other details to be added per row 这是我的当前表格,我想创建一个“高级”选项,该选项允许每行添加其他详细信息

简单表

So then if you click "advanced" it will expand out to these features, as well allowing you to add or delete additional rows 因此,如果您单击“高级”,它将扩展到这些功能,并允许您添加或删除其他行

扩展表

Here is the table code that I am trying to get to with the expanded view: 这是我试图通过扩展视图获取的表代码:

 <table border="0" align="center" cellpadding="5" cellspacing="0"> <tbody> <tr> <th>First Name</th> <th>Last Name</th> <th>Hourly Rate</th> <th>Contract Until</th> <th>Advanced Details</th> </tr> <tr> <td bgcolor="#F2F2F2" style="text-align: center">John</td> <td bgcolor="#F2F2F2" style="text-align: center">Smith</td> <td bgcolor="#F2F2F2" style="text-align: center">$ <input type="number" name="number57" id="number57"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="date" name="date" id="date"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="button" name="button" id="button" value="Advanced"></td> </tr> <tr> <td bgcolor="#F2F2F2" style="text-align: center">&nbsp;</td> <td bgcolor="#F2F2F2" style="text-align: center"> <label for="select">Project ID:</label> <select name="select" id="select"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select></td> <td bgcolor="#F2F2F2" style="text-align: center">$ <input type="number" name="number57" id="number57"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="date" name="date" id="date"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="button" name="button" id="button" value="Add"> / <input type="button" name="button4" id="button4" value="Remove"></td> </tr> <tr> <td style="text-align: center">Bob</td> <td style="text-align: center">Jones</td> <td style="text-align: center">$ <input type="number" name="number58" id="number58"></td> <td style="text-align: center"><input type="date" name="date2" id="date2"></td> <td style="text-align: center"><input type="button" name="button2" id="button2" value="Advanced"></td> </tr> <tr> <td bgcolor="#F2F2F2" style="text-align: center">Tom</td> <td bgcolor="#F2F2F2" style="text-align: center">Collins</td> <td bgcolor="#F2F2F2" style="text-align: center">$ <input type="number" name="number59" id="number59"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="date" name="date3" id="date3"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="button" name="button3" id="button3" value="Advanced"></td> </tr> <tr> <td bgcolor="#F2F2F2" style="text-align: center">&nbsp;</td> <td bgcolor="#F2F2F2" style="text-align: center"> <label for="select">Project ID:</label> <select name="select" id="select"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select></td> <td bgcolor="#F2F2F2" style="text-align: center">$ <input type="number" name="number57" id="number57"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="date" name="date" id="date"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="button" name="button" id="button" value="Add"> / <input type="button" name="button4" id="button4" value="Remove"></td> </tr> <tr> <td bgcolor="#F2F2F2" style="text-align: center">&nbsp;</td> <td bgcolor="#F2F2F2" style="text-align: center"> <label for="select">Project ID:</label> <select name="select" id="select"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select></td> <td bgcolor="#F2F2F2" style="text-align: center">$ <input type="number" name="number57" id="number57"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="date" name="date" id="date"></td> <td bgcolor="#F2F2F2" style="text-align: center"><input type="button" name="button" id="button" value="Add"> / <input type="button" name="button4" id="button4" value="Remove"></td> </tr> <tr> <td style="text-align: center">&nbsp;</td> <td style="text-align: center">&nbsp;</td> <td style="text-align: center">&nbsp;</td> <td style="text-align: center">&nbsp;</td> <td style="text-align: center"><input type="submit" name="submit" id="submit" value="Submit"></td> </tr> </tbody> </table> 

Hard to suggest advice without some example code, but I think this does something similar. 没有一些示例代码,很难提出建议,但是我认为这样做的效果相似。

https://jsfiddle.net/3gkwoe7j/4/ https://jsfiddle.net/3gkwoe7j/4/

One note on dynamically adding elements, you need to attach the event listener to something static in the DOM, and then use a selector. 关于动态添加元素的一个说明,您需要将事件侦听器附加到DOM中的静态对象,然后使用选择器。 (I assume you're using jQuery, since you tagged it in you question) So in my example: (我假设您使用的是jQuery,因为您在问题中标记了它)因此在我的示例中:

$('.table')
    .on('click', 'button.advanced.toggle', function() {
    $(this)
      .text(this.textContent === "LESS" ? "ADVANCED" : "LESS")
      .parents('.person')
        .find('.advanced.section').toggle();
  })

I bind a click listener to the .table and then filter by the selector button.advanced.toggle . 我将点击监听器绑定到.table ,然后通过选择器button.advanced.toggle进行过滤。

Hope that helps. 希望能有所帮助。

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

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