计算具有数量的数据表中的总检查价格(使用每个函数jquery)

时间:2016-07-17 23:42:36

标签: javascript jquery datatables

我是新来的。 我想用数据表中的数量来计算已检查行的价格。 我已经有了代码。我可以计算它但我的问题是, 当我选中复选框并取消选中它然后再次检查,然后我输入数量,结果不对。但是当我刷新并检查并输入数量时,结果是正确的。 这是我的代码

$(document).ready(function () {
var crpackagetbl = $('#crpackagetbl').DataTable({
    "bLengthChange": false,
    "sPaginationType": "full_numbers",
    responsive: true,
    "order": [],
    "columnDefs": [
        {"targets": 'no-sort', "orderable": false},
        {className: "dt-body-left", "targets": [1]},
        {className: "dt-body-center", "targets": [0]}
    ],
    "rowHeight": '10px'
});

$("#crpackageSearch").bind('keyup search input paste cut', function () {
    crpackagetbl.search(this.value).draw();
});

var sum = 0;
var $sum = parseFloat(sum).toFixed(2);
var tots = 0;
var $tots = parseFloat(tots).toFixed(2);
var priceqty = 0;
var $priceqty = parseFloat(priceqty).toFixed(2);
var y = 0;
var $y = parseFloat(y).toFixed(2);
$('.packcheckbox').change(function () {
    var id = $(this).val();
    var dis = $(this);
    console.log(id);
    if ($(this).is(':checked')) {
        var $this = $(this).attr('id');
        console.log($this);
        var $tr = $(this).closest('tr');
        var $qty = $tr.find('td .rowQty');
        var price = $tr.find('td:eq(3)').text();
        var $price = price.replace(/[^\d.]/g, '');
        var $qqq = $qty.val();

        var rowCollection = crpackagetbl.$(".packcheckbox:checked", {"page": "all"});
        rowCollection.each(function (index, elem) {
            var q = $qty.val();
            $qty.keyup(function () {
                var qtyval = $qty.val();
                if (qtyval > q) {
                    $priceqty = (parseFloat($price) * parseFloat(qtyval - q)).toFixed(2);
                    console.log("qtyval: " + qtyval);
                    console.log("priceqty: " + $priceqty);
                    console.log("q: " + q);
                    $tots += parseFloat($priceqty).toFixed(2);
                    console.log("tots: " + $tots);
                    $sum = (parseFloat($sum) + parseFloat($tots)).toFixed(2);
                    console.log("add: " + $sum);
                    $('#crAllCheckedPackPrice').val($sum);
                    return false;
                    $tots = 0;
                    q = qtyval;
                    $priceqty = 0;
                } else if (qtyval < q) {
                    $priceqty = (parseFloat($price) * parseFloat(q - qtyval)).toFixed(2);
                    $tots += parseFloat($priceqty).toFixed(2);
                    $sum = (parseFloat($sum) - parseFloat($tots)).toFixed(2);
                    console.log("minus: " + $sum);
                    $('#crAllCheckedPackPrice').val($sum);
                    $tots = 0;
                    q = qtyval;
                    $priceqty = 0;
                    return false;
                } else {
                    return false;
                }
            });
            $y = (parseFloat($price) * parseFloat($qqq)).toFixed(2);
            $sum = (parseFloat($sum) + parseFloat($y)).toFixed(2);
            $('#crAllCheckedPackPrice').val($sum);
            $y = 0;
            $qqq = 0;
            return false;
        });
    } else if (dis.is(':not(:checked)')) {
        var $untr = dis.closest('tr');
        var unqty = $untr.find('td .rowQty');
        var $unqty = unqty.val();
        console.log("unqty: " + $unqty);
        var unprice = $untr.find('td:eq(3)').text();
        console.log("unprice: " + unprice);
        var unpricenumber = unprice.replace(/[^\d.]/g, '');
        console.log("unpricenumber: " + unpricenumber);
        var unrowCollection = crpackagetbl.$(".packcheckbox:not(:checked)", {"page": "all"});
        console.log(unrowCollection);
        unrowCollection.each(function (index, elem) {

            $tots = (parseFloat(unpricenumber) * parseFloat($unqty)).toFixed(2);
            $sum = (parseFloat($sum) - parseFloat($tots)).toFixed(2);
            $('#crAllCheckedPackPrice').val($sum);
            $tots = 0;
            return false;
        });
    }
});

});

0 个答案:

没有答案