Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript计算小计的10%折扣_Javascript_Php_Discount_Subtotal - Fatal编程技术网

javascript计算小计的10%折扣

javascript计算小计的10%折扣,javascript,php,discount,subtotal,Javascript,Php,Discount,Subtotal,如何在js中实现基于小计的10%折扣计算?。 这是我的密码 <!-- Sub Total --> function calculateGrandTotal() { var grandTotal = 0; $('table tbody tr td:nth-child(4) input').each(function (index) { grandTotal += parseInt($(this).val()

如何在js中实现基于小计的10%折扣计算?。 这是我的密码

<!-- Sub Total -->
function calculateGrandTotal() {
    var grandTotal = 0;
    $('table tbody tr td:nth-child(4) input').each(function (index) {
                                grandTotal += parseInt($(this).val());
    });
    $('#sub_total').val(grandTotal);
}

函数calculateGrandTotal(){
var-grandTotal=0;
$('table tbody tr td:nth child(4)input')。每个(函数(索引){
grandTotal+=parseInt($(this.val());
});
$('小计').val(总计);
}

使用getElementbyID获取小计字段的值并应用公式。

它看起来如下:

function calculateGrandTotal() {
                            var grandTotal = 0;
                            $('table tbody tr td:nth-child(4) input').each(function (index) {
                                grandTotal += parseInt($(this).val());
                            });
                            $('#sub_total').val(grandTotal);
                            $('#discount').val(calculateDiscount(grandTotal));
 } 
//separate function to calculate discount
function calculateDiscount(amt){
   if(!NaN(amt)){
      return amt*0.1;//10% disc
   }else return 0;
}

我的代码是什么样子的?
grandTotal-(grandTotal*.1)
我将添加到代码行?…或者我将有一个新函数?…对不起,我只是JS中的新手,我如何调用该函数以显示在字段中?如果我想有一个用于折扣的函数,因为我想计算小计和折扣以获得一个总计…函数calculateGrandTotal(){var subTotal=0$('table tbody tr td:nth child(4)input').each(function(index){subTotal+=parseInt($(this.val());})$('subTotal').val(subTotal);$('#discount').val(subTotal*0.1)}如果它工作,有什么问题?