我正在做折扣功能,用户可以输入折扣金额,如用户输入10则变为50(商品价格)-10 = 40,如果用户输入10%,结果为50-10%= 45 。
我试过以下代码。值是否包含%符号可能不同,但如果包含%符号则值为NaN。我知道在进行计算时无法识别符号,因此它变为NaN,但如何使其忽略%符号?当我输入折扣时它也会改变。 谢谢。
$("#table_invoice").on("input", '.dt_discount', function() {
calculate_subTotal($(this).closest('.dt_row'));
})
function calculate_subTotal(row) {
var unitCost = $(row).find(".dt_unitPrice").val();
var qty = $(row).find(".dt_qty").val();
var discount = $(row).find(".dt_discount").val();
if (discount.indexOf("%") > -1) {
var row_total = Number(unitCost * qty * (1 - (discount / 100)));
} else {
var row_total = Number((unitCost * qty) - discount);
}
$(row).find(".dt_subtotal").val(row_total);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="table_invoice">
<row class="dt_row">
Price<input class="form-control form-price dt_unitPrice" name="dt_unitPrice" value="50"></br>
Qty<input class="form-control form-quantity dt_qty" name="dt_qty" value="2"></br>
Discount<input class="form-control form-discount dt_discount" name="dt_discount" value="0"></br>
Total<input class="form-control form-price dt_subtotal" name="dt_subtotal" value="0.00">
</row>
</div>
答案 0 :(得分:2)
如果折扣包含百分比,您可以解析折扣。
$("#table_invoice").on("input", '.dt_discount', function() {
calculate_subTotal($(this).closest('.dt_row'));
})
function calculate_subTotal(row) {
var unitCost = $(row).find(".dt_unitPrice").val();
var qty = $(row).find(".dt_qty").val();
var discount = $(row).find(".dt_discount").val();
if (discount.indexOf("%") > -1) {
discount = parseInt(discount, 10); // <-- here
var row_total = Number(unitCost * qty * (1 - (discount / 100)));
} else {
var row_total = Number((unitCost * qty) - discount);
}
$(row).find(".dt_subtotal").val(row_total);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="table_invoice">
<row class="dt_row">
Price<input class="form-control form-price dt_unitPrice" name="dt_unitPrice" value="50"></br>
Qty<input class="form-control form-quantity dt_qty" name="dt_qty" value="2"></br>
Discount<input class="form-control form-discount dt_discount" name="dt_discount" value="0"></br>
Total<input class="form-control form-price dt_subtotal" name="dt_subtotal" value="0.00">
</row>
</div>
&#13;
答案 1 :(得分:1)
您可以使用解决方案https://jsfiddle.net/p5jm98yj/1/
$("#table_invoice").on("input", '.dt_discount', function() {
calculate_subTotal($(this).closest('.dt_row'));
})
function calculate_subTotal(row) {
var unitCost = $(row).find(".dt_unitPrice").val();
var qty = $(row).find(".dt_qty").val();
var discount = $(row).find(".dt_discount").val();
if (discount.indexOf("%") > -1) {
var row_total = Number(unitCost * qty * (1 - (parseFloat(discount) / 100)));
} else {
var row_total = Number((unitCost * qty) - discount);
}
$(row).find(".dt_subtotal").val(row_total);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="table_invoice">
<row class="dt_row">
Price<input class="form-control form-price dt_unitPrice" name="dt_unitPrice" value="50"><br/>
Qty<input class="form-control form-quantity dt_qty" name="dt_qty" value="2"><br/>
Discount<input class="form-control form-discount dt_discount" name="dt_discount" value="0"><br/>
Total<input class="form-control form-price dt_subtotal" name="dt_subtotal" value="0.00">
</row>
</div>
它也适用于十进制值,如12.5%。
答案 2 :(得分:1)
$("#table_invoice").on("input", '.dt_discount', function() {
calculate_subTotal($(this).closest('.dt_row'));
})
function calculate_subTotal(row) {
var unitCost = $(row).find(".dt_unitPrice").val();
var qty = $(row).find(".dt_qty").val();
var discount = $(row).find(".dt_discount").val();
if (discount.indexOf("%") > -1) {
discount=discount.replace("%", '');
discount=parseInt(discount);
var row_total = Number(unitCost * qty * (1 - (discount / 100)));
} else {
var row_total = Number((unitCost * qty) - discount);
}
$(row).find(".dt_subtotal").val(row_total);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="table_invoice">
<row class="dt_row">
Price<input class="form-control form-price dt_unitPrice" name="dt_unitPrice" value="50"></br>
Qty<input class="form-control form-quantity dt_qty" name="dt_qty" value="2"></br>
Discount<input class="form-control form-discount dt_discount" name="dt_discount" value="0"></br>
Total<input class="form-control form-price dt_subtotal" name="dt_subtotal" value="0.00">
</row>
</div>