简体   繁体   English

表格中表格的动态行

[英]Dynamic Rows for Table in a Form

I'm trying to create a form that can dynamically add more rows to a table as needed. 我正在尝试创建一种可以根据需要向表中动态添加更多行的表单。 In the example that I will post you will see a table for chaperones. 在我将发布的示例中,您将看到一个伴侣表。 Since, we do not know off hand how many chaperones each school will bring it needs to be dynamic. 既然如此,我们不知道每所学校将带来多少陪伴者就需要保持活力。 I'm trying to use Javascript to create an onclick button that will add a new row to the table and storing the names as an array so that it can be sent POST and analyzed using PHP and then stored into the database. 我正在尝试使用Javascript创建一个onclick按钮,该按钮将在表中添加新行并将名称存储为数组,以便可以将其发送到POST并使用PHP进行分析,然后存储到数据库中。 I found a solution online, however when I try to implement it on my site nothing is working once I click the button. 我在网上找到了一个解决方案,但是当我尝试在自己的网站上实施该解决方案时,一旦单击该按钮,将无法正常工作。 Maybe I just need another set of eyes to look it over and see if I need anything. 也许我只需要另一只眼睛看一下,看看是否需要任何东西。

  <table id="chp" class="table table-striped"> <tr> <th class="text-center">First Name</th> <th class="text-center">Last Name</th> <th class="text-center">Phone</th> <th class="text-center">Email</th> <th class="text-center"> + </th> </tr> <tr> <td><input id="chpFirst" name="chpFirst" type="text" placeholder="John" class="form-control input-md" required=""></td> <td><input id="chpLast" name="chpLast" type="text" placeholder="Doe" class="form-control input-md" required=""></td> <td><input id="chpPhone" name="chpPhone" type="text" placeholder="5555555555" class="form-control input-md" required=""></td> <td><input id="chpEmail" name="chpEmail" type="text" placeholder="john.doe@email.com" class="form-control input-md" required=""></td> <td><input type="button" id="more_fields" onclick="add_chp();" value="Add" class="btn btn-info" /></td> </tr> </table> <script> function add_chp() { document.getElementByID("chp").insertRow(-1).innerHTML = '<tr>' + '<td><input id="chpFirst" name="chpFirst[]" type="text" placeholder="John" class="form-control input-md" required=""></td>' + '<td><input id="chpLast" name="chpLast[]" type="text" placeholder="Doe" class="form-control input-md" required=""></td>' + '<td><input id="chpPhone" name="chpPhone[]" type="text" placeholder="5555555555" class="form-control input-md" required=""></td>' + '<td><input id="chpEmail" name="chpEmail[]" type="text" placeholder="john.doe@email.com" class="form-control input-md" required=""></td>' + '<td><input type="button" id="more_fields" onclick="add_fields();" value="Add More" class="btn btn-info" /></td>' + '</tr>'; } </script> 

https://jsfiddle.net/owk9ct13/ https://jsfiddle.net/owk9ct13/

JavaScript is case sensitive: document.getElementByID wont work. JavaScript区分大小写: document.getElementByID无法使用。 Replace that with document.getElementById and it should fix your issue. 将其替换为document.getElementById ,它将解决您的问题。

Also as a tip, be sure to check the any error or warning messages in the browser console...it makes debugging the front end way easier. 另外,请确保在浏览器控制台中检查任何错误或警告消息,这使调试前端方法变得更加容易。 您的错误图片

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

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