-1

在此处输入图像描述我有一个表格,我想在其中取出一个金额的百分比,例如。让金额为 100,我将输入 10,因此所需的输出为 10,我什至想将百分比的金额与金额相加,因此输出为 110(100 + 10)我可以取出百分比,但它不是添加到金额中,这些字段将是动态生成的输入。

这是我的代码:

    $(document).ready(function(){
        $('#detail').on('keyup', '.rent, .stperc, .st, .stamt, .cal', calculateRow);

            function calculateRow() {
                     var $row   = $(this).closest('tr');
                     var value  = parseFloat($row.find('.stperc').val());
                     var value2 = parseFloat($row.find('.rent').val());


                     var stamt  =  parseFloat($row.find('.stamt').val((value * value2) / 100));

                     var cost = stamt + value2;
                     console.log(cost);
                     if (isNaN(cost)) {
                         $row.find('.cal').val("0");
                     } else {
                         $row.find('.cal').val(cost);
                     }

            }

    });

这是我的 php 代码,它将生成文本框:

                while ($row = mysql_fetch_object($query)){
                       echo "<tr>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='form-input-rate' name=\"locno_$ctr\" value=\"$row->locno\" $stylen readonly>";
                       echo "</td>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='form-input-rate' name=\"ledger_$ctr\" value=\"$row->custcode\" $stylen readonly>";
                       echo "</td>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='form-input-rate' name=\"name_$ctr\" value=\"$row->name\" $stylev readonly>";
                       echo "</td>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='form-input-rate' name=\"deposit_$ctr\" value=\"$row->deposit\" $stylen >";
                       echo "</td>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='rent form-input-rate' name=\"rent_$ctr\" value=\"$row->rent\" $stylen >";
                       echo "</td>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='stperc form-input-rate' name=\"stperc_$ctr\" value=\"$row->stperc\" $stylen >";
                       echo "</td>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='stamt form-input-rate' name=\"st_$ctr\" value=\"$row->stamt\" $stylen >";
                       echo "</td>";
                       echo "<td align='center'>";
                       echo "<input type='text' class='cal form-input-rate' name=\"total_$ctr\" value=\"$row->totamt\" $stylen readonly>";
                       echo "<input type='hidden' name=\"accode_$ctr\" value=\"$row->accode\">";
                       echo "</td>";
                       echo "</tr>"; 
                       $ctr++;
                }
4

1 回答 1

0

尝试像这样的 FIDDLE

$(document).ready(function(){
    $('#detail').on('keyup', '.rent, .stperc, .st, .stamt, .cal', function(){
        var $row   = $(this).closest('tr');
        var value  = parseFloat($row.find('.stperc').val());
        var value2 = parseFloat($row.find('.rent').val());
        var stamt = (value * value2) / 100;
        $row.find('.stamt').val(stamt);
        var cost = stamt + value2;
        if (isNaN(cost)) {
            $row.find('.cal').val("rasd");
        } else {
            $row.find('.cal').val(cost);
        }

   });

});
于 2013-09-04T11:19:05.130 回答