退出和重新进入时由于某种原因在表中复制动态行
Duplicating dynamic rows in table for some reason when exiting and re-entering?
让我来解释一下它能做什么,应该做什么,不应该做什么。
这基本上是每次单击add row please按钮时向表中添加一行。
它只意味着附加1行,它做得很好,直到你点击完成按钮,使灯箱效果消失。
但如果他们再次回到那里,由于某种原因,效果会重复。
如果你再次退出,然后重新进入,它现在三倍的行。
谁能给我指出正确的方向,为什么会这样?这是一个jsfiddle这样你就能明白我的意思了
html代码如下:
<a href="#" id="quottable">Click here</a>
<div class="model-background">
<div class="model-wrap">
<div class="ubl-section-5">
<div class="table-responsive" id="jquerytable">
<table class="table table-bordered">
<thead>
<tr>
<th class="text-center">Item Description</th>
<th class="text-center">Cost</th>
<th class="text-center">Sell At</th>
<th class="text-center">Quantity</th>
<th class="text-center">Total Cost</th>
<th class="text-center">Total Sell</th>
<th class="text-center">Margin</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" class="form-control" id="jquerytabledescription" value="" placeholder="Unit Item Name">
</td>
<td class="text-center">
<input type="text" class="form-control" id="jquerytablecosts" value="" placeholder="Enter Cost Without £ Symbol">
</td>
<td class="text-center">
<input type="text" class="form-control" id="jquerytablesell" value="" placeholder="Enter Selling Price Without £ Symbol">
</td>
<td class="text-center">
<input type="text" class="form-control" id="jquerytablequantity" value="" placeholder="Quantity">
</td>
<td class="text-center" id="jqueryTotalcost">X</td>
<td class="text-center" id="jquerySellingcost">X</td>
<td class="text-center" id="jquerymargin">X</td>
</tr>
</tbody>
</table>
<p><a href="#" class="btn btn-default btn-block" id="anotherrowplease">Need Another Row Please</a></p>
<p><a href="#" class="btn btn-danger btn-block" id="ubldone4">Done</a></p>
</div>
</div>
</div>
</div>
我使用Bootstrap作为初始css,这里也有一些自定义:
.model-background {
background-color: rgba(0, 0, 0, 0.8);
width: 100%;
height: 100%;
top: 0px;
left: 0px;
position: fixed;
display: none;
}
.model-wrap {
width: 80%;
height: auto;
margin: 100px auto 0px auto;
padding: 40px;
float: none;
background-color: white;
}
.ubl-section-5 {
display: none;
}
最后我运行jQuery 3.1以及这里的代码:
jQuery(document).ready(function($) {
"use strict";
$('#quottable').on('click', function() {
var idName = '';
var lastChar = '';
var jc = '';
var js = '';
var jq = '';
var jfc = '';
var jfs = '';
var jmgn = '';
$('.ubl-section-5').show();
$('.model-background').fadeIn(500);
$.cookie('howMany', 1);
$('#anotherrowplease').on('click', function(event) {
event.preventDefault();
var newRow = '';
var checkHowmany = parseInt($.cookie('howMany'));
if (checkHowmany === 1) {
checkHowmany = 2;
}
$.cookie('howMany', checkHowmany + 1);
newRow += '<tr>';
newRow += '<td><input type="text" class="form-control" id="jquerytabledescription' + checkHowmany + '" value="" placeholder="Unit Item Name"></td>';
newRow += '<td class="text-center"><input type="text" class="form-control" id="jquerytablecosts' + checkHowmany + '" value="" placeholder="Enter Cost Without £ Symbol"></td>';
newRow += '<td class="text-center"><input type="text" class="form-control" id="jquerytablesell' + checkHowmany + '" value="" placeholder="Enter Selling Price Without £ Symbol"></td>';
newRow += '<td class="text-center"><input type="text" class="form-control" id="jquerytablequantity' + checkHowmany + '" value="" placeholder="Quantity"></td>';
newRow += '<td class="text-center" id="jqueryTotalcost' + checkHowmany + '">X</td>';
newRow += '<td class="text-center" id="jquerySellingcost' + checkHowmany + '">X</td>';
newRow += '<td class="text-center" id="jquerymargin' + checkHowmany + '">X</td>';
newRow += '</tr>';
$('#jquerytable tbody').append(newRow);
//calculate each and every item
$('#jquerytable input').change(function() {
//find the parent id so we know
idName = $(this).attr('id');
lastChar = idName.substr(idName.length - 1);
jc = $('#jquerytablecosts' + lastChar).val();
js = $('#jquerytablesell' + lastChar).val();
jq = $('#jquerytablequantity' + lastChar).val();
if (jc == '' || jc == 'undefined' || jc == null) {
jc = 0;
}
if (js == '' || js == 'undefined' || js == null) {
js = 0;
}
if (jq == '' || jq == 'undefined' || jq == null) {
jq = 0;
}
jfc = jc * jq;
jfs = js * jq;
jmgn = 100 - ((jfc / jfs) * 100);
if ($.isNumeric(lastChar) !== false) {
//get the values of the 3 items and then add them to the final results
$('#jqueryTotalcost' + lastChar).html('£' + jfc);
$('#jquerySellingcost' + lastChar).html('£' + jfs);
$('#jquerymargin' + lastChar).html(jmgn.toFixed(2) + '%');
}
});
});
//calculate each and every item
$('#jquerytable input').change(function() {
//find the parent id so we know
idName = $(this).attr('id');
lastChar = idName.substr(idName.length - 1);
jc = $('#jquerytablecosts').val();
js = $('#jquerytablesell').val();
jq = $('#jquerytablequantity').val();
if (jc == '' || jc == 'undefined' || jc == null) {
jc = 0;
}
if (js == '' || js == 'undefined' || js == null) {
js = 0;
}
if (jq == '' || jq == 'undefined' || jq == null) {
jq = 0;
}
jfc = jc * jq;
jfs = js * jq;
jmgn = 100 - ((jfc / jfs) * 100);
if ($.isNumeric(lastChar) === false) {
//get the values of the 3 items and then add them to the final results
$('#jqueryTotalcost').html('£' + jfc);
$('#jquerySellingcost').html('£' + jfs);
$('#jquerymargin').html(jmgn.toFixed(2) + '%');
}
});
$('#ubldone4').on('click', function(ev) {
ev.preventDefault();
$('.model-background').fadeOut(500);
$('.ubl-section-5').hide();
});
});
});
对于刚接触JS的人来说,这是一个常见的错误,一开始很难理解。发生这种情况的原因是因为每次单击$('#quottable')
时,都要重新绑定$('#anotherrowplease')
的单击处理程序。您可以通过多种方式解决这个问题。我会将$('#anotherrowplease')
的点击处理程序移动到$('#quottable')
处理程序的之外,因为这只需要运行一次(不是每次单击$('#quottable')
)。
有时需要将事件侦听器附加到动态创建的内容,例如正在添加的新行的输入。但是,您不希望继续重新绑定到现有的元素。为了解决这个问题,我想说大多数人选择使用事件委托,你也可以使用一个类来跟踪哪些元素已经有侦听器,并在选择器的末尾添加:not(.myClass)
,或者在重新绑定之前简单地调用.off()
。
相关文章:
- 动态添加/复制表单时绑定到表单提交事件
- 我怎么能知道电子邮件是动态复制的呢
- 动态复制元素 ID
- 在提交表格时计算所有动态项目的总数,包括复制的项目
- ExtJS拖放中的动态复制
- 如何将节点内容从 xml 复制到硬编码的 html(动态到原始 html)
- 复制动态上载的图像
- 如何使用带有动态内容和触发器的剪贴板.js进行复制
- 动态复制HTML DIV与Javascript
- 动态添加或复制指令不会从父作用域获取变量值
- 删除:动态地复制/克隆一个表单以获得额外的数据输入
- 通过动态字段中的动态复选框复制地址
- 复制动态画布到另一个画布不工作
- 将动态字段值复制到另一个字段
- Javascript从动态表单中复制字段
- 退出和重新进入时由于某种原因在表中复制动态行
- 如何动态复制数组从php到javascript
- Ext JS 3:如何复制动态生成的树面板
- 动态复制表单文本区域
- 复制动态表列与行隐藏的样式属性textarea jQuery