【问题标题】:jquery percentage calcutaion from the textbox values从文本框值计算jQuery百分比
【发布时间】:2013-09-07 20:28:40
【问题描述】:

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

【问题讨论】:

  • 修改你的代码
  • 看到我也添加了我的 php 代码
  • 我已经发布了我的输出截图

标签: php jquery jquery-calculation


【解决方案1】:

试试这样的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);
        }

   });

});

【讨论】:

  • 抱歉 jquery 版本错误。检查这个jsfiddle.net/rajesh007/6jga8/1
  • 我认为您的问题是正确的,先生,我的问题是百分比的输出没有与金额相加
猜你喜欢
  • 2015-10-11
  • 1970-01-01
  • 2023-02-01
  • 2012-09-16
  • 2018-04-19
  • 2011-06-01
  • 2021-01-10
  • 1970-01-01
相关资源
最近更新 更多