javascript - Javascript附加表行不起作用
问题描述
我花了几个小时来完成这项工作。但是我在这里找不到问题所在,单击按钮时未添加新行,并且控制台未显示任何错误..
目标是为用户提供一个表单,以便在表单中添加一个带有输入字段的额外表格行,或者如果添加太多则删除一个,这样他们就可以一次放入更多行数据。我是 Javascript / Jquery 的新手,欢迎所有帮助!
表格:
<button type="button" name="addrow" id="addrow" class="example_e">Add New Row</button>
<form class="form-horizontal" action="<?php htmlspecialchars($filterpage); ?>" method="post">
<table class="testtable">
<thead>
<tr>
<th style="width:0px !important;"></th>
<th>Account Name</th>
<th>Emailadress</th>
<th>Password</th>
<th>Paying</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="hidden" style="width:2px !important;" class="form-control sl" name="slno[]" id="slno" value="1" readonly=""></td>
<td><input type="text" style="width:190px !important;font-weight: normal !important;"
class="form-control" name="name[]" id="acc_name" placeholder="Enter Account Name" required minlength="3" maxlength="150"></td>
<td><input type="email" style="width:210px !important;font-weight: normal !important;"
class="form-control" name="accmail[]" id="acc_email" placeholder="mail@mail.nl" required minlength="3" maxlength="150"></td>
<td><input type="text" style="width:130px !important;font-weight: normal !important;"
class="form-control" name="password[]" id="acc_pass" placeholder="Enter Password" required minlength="3" maxlength="50"></td>
<td>
<label class="switch">
<input type="checkbox" name="betaald[]" id="acc_betaald" value="Yes" checked/>
<div class="slider round">
<span class="on">Yes</span>
<span class="off">No</span>
</div>
</label></td>
<td></td> </tr></tbody></table>
<br/>
<button type="submit" name="submitnewacc" class="example_e">Add account(s) to database!</button>
</form>
剧本:
<script type="text/javascript">
const tableRow = (i) => {return `
<tr>
<td>
<input type="hidden" class="form-control sl" style="width:2px !important;" name="slno[]" value="${ i }" readonly="">
</td>
<td>
<input type="text" style="width:190px !important;font-weight: normal !important;"
class="form-control" name="name[]" id="acc_name${ i }" placeholder="Enter Account Name"
required minlength="3" maxlength="150" >
</td>
<td><input type="email" style="width:210px !important;font-weight: normal !important;"
class="form-control" name="accmail[]" id="acc_email${ i }" placeholder="mail@mail.nl" required minlength="3"
maxlength="350" ></td>
<td><input type="text" style="width:130px !important;font-weight: normal !important;"
class="form-control" name="password[]" id="acc_pass${ i }" placeholder="Enter Password" required minlength="3" maxlength="150"></td>
<td>
<label class="switch">
<input type="checkbox" name="betaald[]" id="acc_betaald${ i }" value="Yes" checked/>
<div class="slider round">
<span class="on">Yes</span>
<span class="off">No</span></label></td>
<td><input type="button" class="btnRemove example_b" value="Delete"/></td></tr>
`
}
$('#addrow').click(function() {
var length = $('.sl').length;
var i = parseInt(length) + parseInt(1);
var newrow = $('.tbody').append(tableRow(i));
});
// Removing event here
$('body').on('click', '.btnRemove', function() {
$(this).closest('tr').parent().remove()
});
</script>
解决方案
查询中的函数与普通JS中的函数$()
类似。document.querySelector()
在“tbody”选择器之前var newrow = $('.tbody').append(tableRow(i));
放置一个。.
这将返回类名为“tbody”的元素,而不是其标签名。为了通过标签名称获取元素,只需删除前导.
所以将该行更改为var newrow = $('tbody').append(tableRow(i));
推荐阅读
- javascript - v-on:在 Vue 组件内单击不起作用
- node.js - Angular cli卡在新的Win10上
- ios - React Native Unrecognized 字体系列未修复
- visual-studio-code - 如何删除 VS Code 中的左侧垂直线?
- javascript - 尝试通过 jQuery.post() 将变量从输入收音机传递到 PHP
- javascript - Invisible Recaptcha - 未加载站点密钥?
- php - SQL语法只有两种类型的引号问题?
- android - 无法在 Jetpack 示例应用中为 ViewPager 充气
- amazon-web-services - Amazon EMR 和 Spark 流式传输
- prestashop - 迁移 Prestashop 后我有后端的空白页