我需要计算数量乘以单价输入,相等的输入将是小计并对所有小计求和...
我在 JavaScript 中使用了这段代码,它工作正常,但在表中不起作用。
请帮忙。
HTML/PHP:
<table border="0" cellspacing="0" class="table table-bordered table-hover" data-name="cont-table">
<thead style="">
<tr>
<th class="check-this"><input class="check_all" type="checkbox" onclick="select_all()"/></th>
<th class="text-center"> م </th>
<th class="text-center"> اسم الصنف </th>
<th class="text-center"> الوحدة </th>
<th class="text-center"> الكمية </th>
<th class="text-center"> سعر الوحدة </th>
<th class="text-center"> العام المالي </th>
<th class="text-center"> إجمالي قيمة الصنف </th>
</tr>
</thead>
<tr>
<td><input type="checkbox" class="case"/></td>
<td><span id="snum">1.</span></td>
<td><!--<input type="text" id="first_name" name="first_name[]"/>!-->
<select class="select" id="first_name" name="first_name[]">
<?php
$stmt = $db->query("SELECT first_name6 FROM item");
//$stmt = $pdo->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option >" . $row['first_name6'] . "</option>";
}
?>
</select></td>
<td><!--<input type="text" id="last_name" name="last_name[]"/>!-->
<select class="select" id="first_name" name="first_name[]">
<?php
$stmt = $db->query("SELECT first_name7 FROM unit");
//$stmt = $pdo->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option >" . $row['first_name7'] . "</option>";
}
?>
</select></td>
<td><input class="select qty" type="number" id="tamil" name="tamil[]"/></td>
<td><input class="select unit" type="number" id="english" name="english[]" /></td>
<td><!--<input type="text" id="computer" name="computer[]"/>!-->
<select class="select " id="first_name" name="first_name[]">
<?php
$stmt = $db->query("SELECT first_name8 FROM financial");
//$stmt = $pdo->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option >" . $row['first_name8'] . "</option>";
}
?>
</select></td>
<td><input type="text" id="total" class="amount" value="" /></td>
</tr>
</table>
<button id="add_row" type="button" class="delete btn btn-default pull-right">مسح</button>
<button id="add_row" style="margin-right:5px;" type="button" class="addmore btn btn-default pull-right">اضاف صنف</button>
<br>
<br>
<h3 class="table-h3 " style="text-align:center;">اجمالي قيمة العقد</h3>
<input type="text" name="totalprice" class="result" value="" />
这是 JavaScript 代码:
$(".qty").on('input', function () {
var self = $(this);
var unitVal = self.next().val();
self.next().next().val(unitVal * self.val());
fnAlltotal();
});
$(".unit").on('input', function () {
var self = $(this);
var qtyVal = self.prev().val();
self.next().val(qtyVal * self.val());
fnAlltotal();
});
function fnAlltotal(){
var total=0
$(".amount").each(function(){
total += parseFloat($(this).val()||0);
});
$(".result").val(total);
}
最佳答案
您的 JavaScript 代码不会将事件处理程序附加到任何尚不存在的 input
元素,而仅附加到存在于以下位置的一对文本 input
:页面加载。
当您添加行时,这些新 input
元素上的 input
事件将不会被捕获。为此,您应该使用 event delegation .
其次,您的 input
元素都是其父 td
元素的单个子元素,因此当您对它们应用 next
方法时,不会返回任何东西。
相反,您可以检索正在触发事件的表行,然后从该行中的 .unit
和 .qty
输入读取值,然后更新同一行中的 .amount
元素:
// Use event delegation to capture events on rows that will only exist in future
$(document).on('input', '.qty, .unit', function (e) { // get event
// get number of row this event is occurring in:
var row = $(e.target).closest('tr').index();
// get both unit and qty from that row:
var unitVal = $('.unit').eq(row).val();
var qtyVal = $('.qty').eq(row).val();
// update the amount in that row:
$('.amount').eq(row).val(unitVal * qtyVal);
// adapt total (no change to your code)
fnAlltotal();
});
关于javascript - 计算数量乘以单价输入,相等的输入将进行小计并将所有小计相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40061273/