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条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退