[英]Input values not adding on keyUp jquery
I have a running code snippet that dynamically adds input boxes to a form that is working so fine, however, am trying to get totals of the values from the input boxes but nothing seem to be working. 我有一个正在运行的代码段,该代码段将输入框动态添加到运行良好的表单中,但是,我试图从输入框中获取所有值的总和,但似乎无济于事。 The total area is only showing 0 but not giving me correct results. 总面积仅显示0,但没有给我正确的结果。
Here is my code: 这是我的代码:
JAVASCRIPT TO DISPLAY THE TABLE WITH FORM 用表格显示表格的JAVASCRIPT
$(document).ready(function(){
$(document).on('click', '.add', function(){
var html = '';
html += '<tr>';
html += '<td><input type="text" name="item_name[]" class="form-control item_name" /></td>';
html += '<td><input class="qty" type="text" value="0" data-cubics="500"/></td>';
html += '<td><select name="item_unit[]" class="form-control item_unit"><option value="">Select Unit</option><?php echo fill_unit_select_box($connect); ?></select></td>';
html += '<td width="33%"><span class="cubics"></span>';
html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
$('#item_table').append(html);
});
JAVASCRIPT TO DO THE MATH 用JAVASCRIPT做数学
<script>
function total() {
var total1 = 0;
$('span.cubics').each(function () {
var n = parseFloat($(this).text());
total1 += isNaN(n) ? 0 : n;
});
$('.totalcubics').text(total1.toFixed(2));
}
$('input.qty').keyup(function () {
var val = parseFloat($(this).val());
val = (val ? val * $(this).data('cubics') : '');
$(this).closest('td').next().find('span.cubics').text(val);
total();
});
var $form = $('#insert_form'),
$summands = $form.find('input.qty'),
$sumDisplay = $('span#totalquantity');
$form.keyup(function () {
var sum = 0;
$summands.each(function () {
var value = Number($(this).val());
if (!isNaN(value)) sum += value;
});
$sumDisplay.text(sum);
});
$('input').on({
focus: function () {
if (this.value == '0') this.value = '';
},
blur: function () {
if (this.value === '') this.value = '0';
}
});
</script>
HTML CODE TO DISPLAY TOTALS HTML代码显示总计
<tr class="bg">
<td width="33%">TOTAL</td>
<td width="33%"><span id="totalquantity"></span>
</td>
<td width="33%"><span class="totalcubics"></span>
</td>
</tr>
Any assistance to my problem is highly appreciated 对我的问题的任何帮助都将受到高度赞赏
You need to recreate $summands
each time you add a row to the table. 每次向表中添加一行时,都需要重新创建$summands
。 Insert the line to do so after adding the row. 在添加行后插入行。
$(document).ready(function() { $(document).on('click', '.add', function() { var html = ''; html += '<tr>'; html += '<td><input type="text" name="item_name[]" class="form-control item_name" /></td>'; html += '<td><input class="qty" type="text" value="0" data-cubics="500"/></td>'; html += '<td><select name="item_unit[]" class="form-control item_unit"><option value="">Select Unit</option><?php echo fill_unit_select_box($connect); ?></select></td>'; html += '<td width="33%"><span class="cubics"></span>'; html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>'; $('#item_table').append(html); $summands = $form.find('input.qty'); //*************** add this line }); function total() { var total1 = 0; $('span.cubics').each(function () { var n = parseFloat($(this).text()); total1 += isNaN(n) ? 0 : n; }); $('.totalcubics').text(total1.toFixed(2)); } $('input.qty').keyup(function () { var val = parseFloat($(this).val()); val = (val ? val * $(this).data('cubics') : ''); $(this).closest('td').next().find('span.cubics').text(val); total(); }); var $form = $('#insert_form'), $summands = $form.find('input.qty'), $sumDisplay = $('span#totalquantity'); $form.keyup(function () { var sum = 0; $summands.each(function () { var value = Number($(this).val()); if (!isNaN(value)) sum += value; }); $sumDisplay.text(sum); }); $('input').on( { focus: function () { if (this.value == '0') this.value = ''; }, blur: function () { if (this.value === '') this.value = '0'; } }); }); // end of document ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="insert_form" onsubmit="return false"> <table> <tr class="bg"> <td width="33%">TOTAL</td> <td width="33%"><span id="totalquantity"></span></td> <td width="33%"><span class="totalcubics"></span></td> </tr> </table> <button type="button" class="add">add</button> <table id="item_table"> </table> </form>
At the moment you are creating summands collection with no elements, before any rows have been added. 目前,您正在创建不包含任何元素的summands集合,然后再添加任何行。
I think your problem in 我认为你的问题在
$(this).closest('td').next().find('span.cubics').text(val);
this mean go to the next td
and find the span
and there is no span in the next td
.. you can use 这意味着进入下一个td
,并找到span
并且在接下来的无跨度td
..你可以使用
$(this).closest('tr').find('span.cubics').text(val);
or 要么
$(this).closest('td').next().next().find('span.cubics').text(val);
and while you append the row dynamically you need to use 在动态附加行的同时,您需要使用
$(document).on('keyup' , 'input.qty' , function () {
instead of 代替
$('input.qty').keyup(function () {
With the help of @tracktor53, i managed to get a fix for my problem, below is my updated code; 在@ tracktor53的帮助下,我设法解决了我的问题,下面是我的更新代码;
$(document).on('click', '.add', function(){
var html = '';
html += '<tr>';
html += '<td><input type="text" name="item_name[]" class="form-control item_name" /></td>';
html += '<td><select name="item_unit[]" class="form-control item_unit"><option value="">Select Unit</option><?php echo fill_unit_select_box($connect); ?></select></td>';
html += '<td><input class="qty" type="text" value="0"/></td>';
html += '<td width="33%"><span class="cubics"></span>';
html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
$('#item_table').append(html);
$summands = $form.find('input.qty'); //*************** added this line
});
And with @mohamed's help, this line $(this).closest('td').next().find('span.cubics').text(val);
在$(this).closest('td').next().find('span.cubics').text(val);
的帮助下,这一行$(this).closest('td').next().find('span.cubics').text(val);
was looking for the next <td>
which in actual sense, the next <td>
was an input field but not the one with <span id='cubics'></span>
so, i had to adjust that to this; 正在寻找下一个<td>
,实际上,下一个<td>
是输入字段,但不是具有<span id='cubics'></span>
的输入字段,因此,我不得不对此进行调整。
html += '<td><input class="qty" type="text" value="0"/></td>';
html += '<td width="33%"><span class="cubics"></span>';
Then after i had to use 然后我不得不使用
$(document).on('keyup' , 'input.qty' , function () {
instead of 代替
$('input.qty').keyup(function () {
Now everything is working fine only that i want to multiply select option
values with qty input
from this line; 现在一切都很好,只有我想将select option
值与这一行的qty input
相乘;
html += '<td><select name="item_unit[]" class="form-control item_unit"><option value="">Select Unit</option><?php echo fill_unit_select_box($connect); ?></select></td>'
Any further assistance about this is highly welcome 非常欢迎对此提供任何进一步的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.