jquery - 使用 jQuery 动态添加/删除行
问题描述
我正在使用 jQuery 动态添加/删除行。
jQuery代码如下:
$(document).ready(function() {
var i = 1;
$('#add_row').click(function() {
$('#addr' + i).html("<td> " + (i + 1) + " </td><td><input class='tbl_text' maxlength='250' name='invoiceNo[]' size='25' style='text-transform:uppercase' type='search' required /></td><td><input class='date' max='2099-12-31' name='date[]' onkeydown='return false' type='date' required /></td><td><input class='tbl_text' maxlength='500' name='desc[]' size='50' type='search' required /></td><td><input class='amt' name='amt[]' step='0.01' type='number' required /></td>");
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
i++;
});
$('#delete_row').click(function() {
if (i > 0) {
$('#addr' + i).remove();
calculateSum();
i--;
}
});
});
HTML如下:
<table align="center" border="1" class="table table-bordered table-hover" id="tab_logic" style="border:2px solid black;border-collapse:collapse">
<thead>
<tr>
<th width="25" />
<th>Invoice No.</th>
<th>Invoice Date</th>
<th>Description</th>
<th>Amount S$<br>(Including GST)</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td> 1 </td>
<td align="center"><input class="tbl_text" maxlength="250" name="invoiceNo[]" size="25" style="text-transform:uppercase" type="search" required /></td>
<td align="center"><input class="date" max="2099-12-31" name="date[]" onkeydown="return false" type="date" required /></td>
<td align="center"><input class="tbl_text" maxlength="500" name="desc[]" size="50" type="search" required /></td>
<td align="center"><input class="amt" id="amt" name="amt[]" step="0.01" type="number" required /></td>
</tr>
<tr id='addr1'></tr>
</tbody>
<tr>
<td colspan="5" height="5" style="border:none;" />
</tr>
<tr>
<td align="right" colspan="5" height="50" style="border:none;"><button class="button" id="add_row" type="button">Add Row</button><span style='display:inline-block;width:1em;'> </span><button class="button_cancel" id="delete_row" type="button">Delete Row</button><span style='display:inline-block;width:0.5em;'> </span></td>
</tr>
<tr>
<td colspan="5" height="5" style="border:none;" />
</tr>
<tr height="50" id="summation">
<td class="label" colspan="4"> Total Amount Payable</td>
<td align="right" class="label"><span id="sum">$0.00</span></td>
</tr>
</table>
它正在工作,但有时,我需要在执行该功能之前单击按钮两次,有时我只需要单击一次。
我错过了什么吗?请有什么建议,谢谢。
解决方案
推荐阅读
- reactjs - 将对象传递给导航/.map 对象单页组件
- javascript - 怎么办,如果多值改变然后事件发生
- selenium - 无法在 selenium 中使用 xpath 识别元素
- google-visualization - 将数组传递给谷歌图表日历时,n.getFullYear 不是函数错误
- orientdb - OrientDB getVertex 方法给出空结果
- android - Jsoup 的 java.lang.RuntimeException 错误
- php - 在 WordPress 中使用 ACF 转发器显示特定的博客文章
- c - `WM_MOUSEMOVE` 中的奇怪坐标
- mysql - 有没有办法查看特定列的分类内容?
- c# - RabbitMQ C# AMQP 在尝试连接本地主机时被中断