我创建了jQuery自动完成功能。自动计算或自动求和是成功的,但是问题是当我更改输入(使用自动完成)时,它会继续求和。我该如何解决?enter image description here
<script type="text/javascript">
$(function () {
var len = $("#sts_dtl > tbody > tr").length;
var total = parseFloat($("#xtotal_MT").val());
for (i = 0; i <= len; i++) {
(function(i) { // start IIFE
$("#xcoil_no-"+i).autocomplete({
minLength: 0,
source: '<?php echo base_url('Inventory_Controller/search_coil1'); ?>',
focus: function(event,ui) {
return false;
},
select: function( event, ui ) {
$(this).val(ui.item.coil_no);
$('#xcoil_no-'+i).val(ui.item.coil_no);
$('#xdescription-'+i).val(ui.item.stock_name);
$('#xindex-'+i).val(ui.item.index);
$('#xmt-'+i).val(ui.item.mt);
$('#xmt-'+i).val(ui.item.mt);
$('#xmt-'+i).each(function() {
if (this.value){
total += parseFloat($(this).val() ? $(this).val() : 0);
}
});
console.log( parseFloat(total) );
return false;
}
}
.autocomplete( "instance" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<div>" + item.coil_no + "</div>" )
.appendTo( ul );
};
})(i); // end IIFE
}
});
</script>