【发布时间】:2018-01-15 01:23:24
【问题描述】:
我正在尝试创建一个可以根据需要向表中动态添加更多行的表单。在我将发布的示例中,您将看到一个伴侣表。因为,我们不知道每所学校会带来多少监护人,所以需要保持活力。我正在尝试使用 Javascript 创建一个 onclick 按钮,该按钮将向表中添加一个新行并将名称存储为一个数组,以便可以将其发送 POST 并使用 PHP 进行分析,然后存储到数据库中。我在网上找到了一个解决方案,但是当我尝试在我的网站上实施它时,一旦我单击按钮,就没有任何效果。也许我只是需要另一双眼睛来看看它,看看我是否需要什么。
<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>
【问题讨论】:
-
应该是
document.getElementById("chp")
标签: javascript forms dynamic html-table