从文本框值计算jquery百分比

时间:2013-09-04 11:01:39

标签: php jquery jquery-calculation

enter image description here我有一个表单,其中我想取出一个金额的百分比,例如。让金额为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++;
                }

1 个答案:

答案 0 :(得分: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);
        }

   });

});