douyuan6490 2014-05-05 05:44
浏览 127
已采纳

通过添加动态文本框填充文本框的值

So I'm attempting to add all textboxes that are dynamically generated to get a subtotal.

here is my code that generates the boxes:

<table id="billing-table">
            <tr>
                <th>Service</th><th>Price</th><th>Subtotal</th>
            </tr>
            <?PHP
             $aService = $_POST['service'];
             $N = count($aService);


         for($i=0; $i < $N; $i++)
         {
              $services=MYSQLI_QUERY($con, "SELECT * FROM `Service` WHERE Service_ID = '$aService[$i]'")or die(mysqli_error($con));
              $getservices = mysqli_fetch_array($services);
              $serviceid = $getservices['Service_ID'];
              $servicedesc = $getservices['Service_Type'];
              $serviceprice = $getservices['Service_Cost'];
      echo'<tr>
                <td><input type="text" name="service" value="'. $servicedesc. '"></td>
                <td><input type="text" name="price" id="price'.$i.'" value="'. $serviceprice. '"></td>
                <td><input type="text" class="qty" value="'. $serviceprice. '"></td>

           </tr>';
    } ?>


            <tr>
                <td style="position:absolute;left:5%;">Subtotal</td>
                <td></td>
                <td><input type="text" id="total" value=""></td>

            </tr>
        </table>

I'm attempting to use jquery to do this based off of this fiddle: http://jsfiddle.net/LJKjR/478/

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
       $('input.qty').change(function() {   

    // Loop through all input's and re-calculate the total
    var total = 0;
    $('input.qty').each(function(){
        total += parseInt(this.value);
    });

    // Update the total
    $('#total').val(total);
});
    </script>
</head>

I have everything in place with no errors, but no total value of all prices combined in my subtotal textbox? what am i doing wrong?

  • 写回答

2条回答 默认 最新

  • drrog9853 2014-05-05 06:03
    关注

    You have to put your script inside document.ready function

    $(document).ready(function(e) {
    
        // Commenting the below line because you want in on the fly
        //$('input.qty').change(function() {  
    
            // Loop through all input's and re-calculate the total
            var total = 0;
            $('input.qty').each(function(){
                total += parseInt(this.value);
            });
    
            // Update the total
            $('#total').val(total);
    
    
        //});
    
    });
    

    And if you want the decimals too, then you should use parseFloat instead if parseInt.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证