doutan1857 2014-03-25 10:05
浏览 82
已采纳

无法在javascript中进行乘法运算

i create a page which two textbox automatically multiply show result in another textbox. But it not working.

Javascript for multiplication......

<script  type="text/javascript">
$(document).ready(function() {

    $('#dataTable').on('keyup', '.price', calTotal)
                  .on('keyup', '.quantity', calTotal);

// find the value and calculate it

    function calTotal() {
        var $row = $(this).closest('tr'),
            price    = $row.find('.price').val(),
            quantity = $row.find('.quantity').val(),
            total    = price * quantity;

// change the value in total

        $row.find('.txt').val(total)
    }

});
</script>

the html and php code is...

    <table border="1" id="dataTable">

    <tr>
    <th> Id</th>
    <th>p</th>
    <th>B</th>
    <th>M</th>
    <th>D</th>
    <th>De</th>
    <th>Q</th>
    <th>T</th>
    </tr>
    <?php
    echo"<p></p>";
    echo"<p></p>";
    echo"<p></p>";
    echo"<p><h3> Details</h3></p>";
          $order = "SELECT * FROM abd ";
          $result = mysql_query($order);

         while($row = mysql_fetch_array($result))
         {
$p= $row['p'];
  $b=$row['b'];
  $m= $row['m'];
  $dp=$row['price'];
  $q=$row['quantity'];


        echo "<td><input type=text value=$id name=item[] readonly=readonly /></td>";
        echo "<td><input type=text id=p name=p[] value=$p ></td>";
        echo "<td><input type=text id=B name=b[] value=$b ></td>";
        echo "<td><input type=text id=M name=m[] value=$m ></td>";
        echo "<td><input type=text id=Q name=qua[] value=$qua ></td>";
        echo "<td><input type=text id=price name=price[] value=$price class=price ></td>";
        echo "<td><input type=text id=Quantity name=q value=$q class=quantity ></td>";
        echo "<td><input type=text id=txt name=txt  class=txt  > </td>";
    ?>
    </table>

when i enter dprice and quantity it automatically show the value in total price. But It not working.Please Help

  • 写回答

3条回答 默认 最新

  • doulaopu2343 2014-03-25 10:08
    关注

    Try

    total = parseInt(price) * parseInt(quantity);
    

    This will convert your val() strings to integers to make possible math operations. If have floats use parseFloat() instead parseInt();

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

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了