我有一个代码,应该可以在一个列中总结所有的td。但问题是,sum不会在keyup上自动更新,
这是密码

<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">MASUKKAN SETORAN UNTUK = <b><?php echo $sr; ?></b> ~ <?php echo $flightnum; ?></h4>
    </div>
        <div class="modal-body">
            <table class="display" cellspacing="0" width="100%" id="paymentTable">
                <thead>
                    <th style="width: 1%;">NO</th>
                    <th style="width: 30%;">MATA UANG</th>
                    <th>RATE</th>
                    <th>SETORAN</th>
                    <th>TOTAL IDR</th>
                </thead>
                <tbody>
                <?php
                $i = 1;
                    while ($row = $result->fetch_array()) {
                        echo "<tr>";
                            echo "<td style='width: 1%; '>$row[curr_id]</td>";
                            echo "<td>$row[curr_code] - $row[curr_desc]</td>";
                            echo "<td><input type='hidden' value='$row[conv_rate]' id='convRate$i'>$row[conv_rate]</td>";
                            echo "<td><input type='text' onkeyup='processCurr($i);' id='inputCurr$i'/></td>";
                            echo "<td class='innertd'><div id='calculatedCurr$i' class='calculatedCurr'></div></td>";
                        echo "</tr>";
                        $i++;
                    }
                ?>
                </tbody>
            </table><br/>TOTAL = <div id='totalSumPayment'></div>
            <button class="btn btn-block btn-primary">SUBMIT <b><?php echo $sr; ?></b> ~ <?php echo $flightnum; ?> DATA</button>
        </div>
</div>

<script>
    $('#paymentTable').dataTable();
    function processCurr(param){
        var inputcurr= $('#inputCurr'+param).val();
        var conversion = $('#convRate'+param).val();
        $('#calculatedCurr'+param).html(inputcurr * conversion);
        calculateSum();
    }

    function calculateSum(){
        var sum = 0;
        //iterate through each textboxes and add the values
        $(".calculatedCurr").each(function(key,value) {
            //add only if the value is number
            if (!isNaN(value) && value.length != 0) {
                sum += parseFloat(value);
                $(this).css("background-color", "#FEFFB0");
            }
            else if (value.length != 0){
                $(this).css("background-color", "blue");
            }
        });

            $("#totalSumPayment").html(sum);
    }
</script>

我不知道是什么问题,一切似乎都很好。请帮帮我,。,

最佳答案

如果你尝试使用类型转换,那可能是个问题。
听说我们没有和数组,所以在格式化时不需要获取key和value
只需简单地获取内部html,如bellow for value

 function processCurr(param){
    var inputcurr= $('#inputCurr'+param).val();
    var conversion = $('#convRate'+param).val();
    $('#calculatedCurr'+param).html(inputcurr * conversion);
    calculateSum();
}

function calculateSum(){
    var sum = parseFloat(0);
    $(".calculatedCurr").each(function(){
        //Hear we dont have and array so it's not required to get key, value while itratting .each
        // just simply get inner html like bellow as value
        $val=parseFloat($(this).html());
        if (!isNaN($val) && $val.length != 0) {
            sum =sum + parseFloat($val);
            $(this).css("background-color", "#FEFFB0");
        }
        else if ($val.length != 0){
            $(this).css("background-color", "blue");
        }

    });

    $("#totalSumPayment").html(sum);
}

关于javascript - 总结TD onkeyup,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36674015/

10-10 08:26