|
|
|
ช่วยแก้ code script การคำนวนให้ทีครับพอทำแล้วไม่ยอมคำนวน ขอบคุณครับ |
|
|
|
|
|
|
|
ตั้งแต่เขียน jQuery ก็ไม่ค่อยได้จับ DOM
ก็ยังแนะนำ jQuery เพราะโดยส่วนตัวแล้ว คิดว่าง่ายกว่ากันเยอะ
Code (JavaScript)
var amount = parseFloat($("#amount").val());
var price = parseFloat($("#price").val());
if (amount != price) {
$("#id2").val(amount * price);
}
else {
$("#total").val(0);
}
|
|
|
|
|
Date :
2016-06-08 16:44:56 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<input type='hidden' name='price[<?= $i ?>]' id='price<?= $i ?>' class='Price' value='<?= $objResult["price"] ?>'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เปลี่่ยน ID เป็น class
|
|
|
|
|
Date :
2016-06-09 07:59:06 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองปรับใช้ดูนะ
Code (PHP)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<table>
<?php
$price = 5;
$amount = 5000;
for ($i = 1; $i <= 5; $i++) {
$price += 5;
$amount -= 10;
$total = floatval($price) * floatval($amount);
?>
<tr>
<td><?php echo $i;?></td>
<td><input name='price[<?php print $i;?>]' id='price<?php print $i?>' class='price' value="<?php echo number_format($price);?>"></td>
<td><input type='text' name='amount[<?php print $i;?>]' id='amount[<?php print $i;?>]' class='amount' value="<?php echo number_format($amount);?>"></td>
<td><input type='text' name='Total' class="total" value="<?php echo number_format($total);?>" readonly></td>
</tr>
<?php
}
?>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".amount, .price").change(function(){
amount = parseFloat($(this).closest('tr').find('.amount').val().replace (/,/g, ""));
price = parseFloat($(this).closest('tr').find('.price').val().replace (/,/g, ""));
$(this).closest('tr').find('.total').val((amount * price).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
$(this).closest('tr').find('.amount').val(amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
$(this).closest('tr').find('.price').val(price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
});
});
</script>
</body>
</html>
|
|
|
|
|
Date :
2016-06-09 08:58:33 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|