Ongkir +小计Javascript

时间:2017-03-02 07:33:25

标签: javascript php

我有发票表格。一切正常但我在文本输入id='ongkir'中有一个问题。如果我在文本输入 ongkir 上输入值,则文本输入 grandtotal 必须是 subtotal + ongkir 。但它不起作用。

检查此代码(auto.js):

$(document).on('change keyup blur', '#ongkir', function () {
    calculateTotal();
});

//total price calculation 
function calculateTotal() {
    subTotal = 0; total = 0;
    $('.totalLinePrice').each(function () {
        if ($(this).val() != '') subTotal += parseFloat($(this).val());
    });
    $('#subTotal').val(subTotal.toFixed(2));
    ongkir = $('#ongkir').val();
    if (ongkir != '') {
        ongkir = $('#ongkir').val();
        total = subTotal + ongkir;
        $('#grandtotal').val(total.toFixed(2));
    }
    else {
        total = subTotal;
    }
    $('#grandtotal').val(total.toFixed(2));
    calculateAmountDue();
}

这是文件transaksi.php:

<div class="col-lg-7">
    <div class="form-horizontal">
        <div class="form-group">
            <label for="subtotal" class="col-sm-7 text-right control-label"><b class="pull-right">Subtotal: </b></label>
            <div class="col-sm-5">
                <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">Rp</span>
                    <input type="number" class="form-control" id="subTotal" placeholder="Subtotal"
                           onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" readonly>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="ongkir" class="col-sm-7 control-label"><b class="pull-right">Ongkir: </b></label>
            <div class="col-sm-5">
                <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">Rp</span>
                    <td><input type="text" name="ongkir" id="ongkir" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="grandtotal" class="col-sm-7 control-label"><b class="pull-right">Grand Total: </b></label>
            <div class="col-sm-5">
                <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">Rp</span>
                    <input type="number" class="form-control" id="grandtotal" placeholder="Total" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" readonly>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="bayar" class="col-sm-7 control-label"><b class="pull-right">Bayar: </b></label>
            <div class="col-sm-5">
                <div class="input-group">
                    <span class="input-group-addon" id="basic-addon1">Rp</span>
                    <input type="number" class="form-control" id="bayar" placeholder="Jumlah Bayar" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="subtotal" class="col-sm-7 control-label"><b class="pull-right">Tanggal Bayar: </b></label>
            <div class="col-sm-5">
                <div class="input-group">
                    <input type="text" class="form-control" placeholder="Tanggal Bayar" aria-describedby="basic-addon1">
                    <span class="input-group-addon" id="basic-addon1"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

这有用吗?如果没有那么多,我可以在这里看到错误的东西(除<td>之外)...

function calculateTotal() {
    var subTotal = 0,
        total = 0;
    $('.totalLinePrice').each(function() {
        if ($(this).val() != '') subTotal += parseFloat($(this).val());
    });
    $('#subTotal').val(subTotal.toFixed(2));
    var ongkir = $('#ongkir').val();
    if (ongkir != '') {
        total = subTotal + parseFloat(ongkir);
        $('#grandtotal').val(total.toFixed(2));
    } else {
        total = subTotal;
    }
    $('#grandtotal').val(total.toFixed(2));
    calculateAmountDue();
}

如果这不起作用,我会检查&#34; calculateAmountDue()&#34;中的内容。