【发布时间】:2020-03-13 18:11:14
【问题描述】:
如何在初始时根据基本价格计算折扣数组,然后根据折扣后金额计算?
在下面的屏幕截图中,我有基本价格。这个底价可以有多个折扣。
每个折扣只能在计算出的基本价格之后提供。
我的 PHP 脚本如下;
<tbody>
<div class="form-group">
<label class=\"col-sm-3\" style='color: green'><strong>Base Price</strong></label>
<div class='col-sm-8'>
<input type='number' class='form-control' id='basePrice' name="basePrice" value='5985'>
</div>
</div>
<?php
$query = $connect->prepare("SELECT * FROM `manage_customers_discount` WHERE status = 1 AND company_id = '1' AND discount_is_deleted = 0");
$query->execute();
$query->store_result();
$rows = $query->num_rows;
$rows = $rows + 1;
$arrayDiscountNumber = 0;
for ($y = 1; $y < $rows; $y++) { ?>
<tr id="row<?php echo $y; ?>" class="<?php echo $arrayDiscountNumber; ?>">
<td style="margin-left:20px;">
<div class="form-group">
<select class="form-control" name="discountName[]" id="discountName<?php echo $y; ?>" onchange="getDiscountData(<?php echo $y; ?>)">
<option value="">~~SELECT~~</option>
<?php
$discountSql = "SELECT * FROM `manage_customers_discount` WHERE status = 1 AND company_id = '1' AND discount_is_deleted = 0 ORDER BY discount_order ASC";
$discountData = $connect->query($discountSql);
while ($row = $discountData->fetch_array()) {
echo "<option value='" . $row['id'] . "' id='changeDiscount" . $row['id'] . "'>" . $row['discount_name'] . "</option>";
} // /while
?>
</select>
</div>
</td>
<td style="padding-left:20px;">
<input type="text" name="rateDiscount[]" id="rateDiscount<?php echo $y; ?>" autocomplete="off" disabled="true" class="form-control" />
<input type="hidden" name="rateDiscountValue[]" id="rateDiscountValue<?php echo $y; ?>" autocomplete="off" class="form-control" />
</td>
<td style="padding-left:20px;">
<input type="text" name="totalDiscount[]" id="totalDiscount<?php echo $y; ?>" autocomplete="off" class="form-control" disabled="true" />
<input type="hidden" name="totalDiscountValue[]" id="totalDiscountValue<?php echo $y; ?>" autocomplete="off" class="form-control" />
</td>
<td style="padding-left:20px;">
<input type="text" name="amountAfterDiscount[]" id="amountAfterDiscount<?php echo $y; ?>" autocomplete="off" class="form-control" disabled="true" />
<input type="hidden" name="amountAfterDiscountValue[]" id="amountAfterDiscountValue<?php echo $y; ?>" autocomplete="off" class="form-control" />
</td>
<td>
<button class="btn btn-default removeDiscountRowBtn" type="button" id="removeDiscountRowBtn" onclick="removeDiscountRow(<?php echo $y; ?>)"><i class="glyphicon glyphicon-trash"></i></button>
</td>
</tr>
<?php
$arrayDiscountNumber++;
} // /for
?>
</tbody>
正如您在屏幕截图中看到的,我计算了基价,它给了我折扣后的第一个金额Base Price * 15%。在第二个折扣中,我需要计算First Amount After Discount * 10%。与第三次折扣相同。折扣后的最后一个金额将显示在子折扣金额中。
下面是我的JS,
var subTotalValue = $('#basePrice').val();
$("#rateDiscount" + row).val(response.percentage);
$("#rateDiscountValue" + row).val(response.percentage);
var total = Number(response.percentage) * Number(subTotalValue);
total = total.toFixed(2);
$("#totalDiscount" + row).val(total);
$("#totalDiscountValue" + row).val(total);
var total = Number(subTotalValue) - Number(total);
total = total.toFixed(2);
$("#amountAfterDiscount" + row).val(total);
$("#amountAfterDiscountValue" + row).val(total);
问题:它正在计算相对于基本价格的所有百分比
【问题讨论】:
标签: javascript percentage