如何减去输入值

时间:2013-07-03 12:15:45

标签: jquery phpmyadmin

我有一个php页面,我可以从中减去两个值,其中一个是从mysql数据库中获取的,另一个是由用户自己输入的。我想要的是在输入第二个输入字段中的值时,应该从数据库中调用的第一个输入值中减去输入的值,并且减去的值应该在另一个输入中可见... 代码是......

  <html>
      <head>
        <style>
            td {font-size:13px;}
        </style>
        <script src="jquery-1.8.3.js"></script>
        <script type="text/javascript">
          $(function(){           
              $('input').each(function() {
                    $(this).keyup(function(){  
                         calculatedis($(this));
                      });
                });
           });

          function calculatedis(src) {
          var sub = 0;
          var sumtable = src.closest('.sumtable');
          sumtable.find('input').each(function() {
                var amt1 = $('input[name="amt1"]').val();
                var amt2 = $('input[name="amt2"]').val();
                sub = (amt1 - amt2)
              });
          sumtable.find(".netAmt").val(sub.toFixed(2));
          }   
         </script> 
      </head>
      <body>
         <form class="inward">
         <?php
            include("dbconnection.php");
            $sql = "SELECT id,price FROM table1 WHERE id='3' ORDER BY id";
            $res = mysql_query($sql) or die(mysql_error());
            echo "<table border='1' align='center' cellspacing='0' cellpadding='3' class='sumtable'>";

             echo "<tr bgcolor='#CCCCCC' height='30'>";
            echo "<td>Price</td>";
            echo "<td>Discounted Amt</td>";
            echo "<td>Net Amt</td>";
             echo "</tr>";
            while($row2=mysql_fetch_array($sql2))
            {
            //change row color alternately  
            $i++;
            $color = ($i%2) ? "#f8f8ff" : "#f0fff0";
            echo "<tr bgcolor=" . $color . " onmouseover='ChangeBackgroundColor(this)' onmouseout='RestoreBackgroundColor(this)'>
                $price = $row2["price"];
            if (strlen(trim($price)) == 0)
            {
            echo "<td>&nbsp;</td>";
            }
            else
            {
            echo "<td valign='top'><input name='amt1' value=" . $price . " size='5'/></td>";
            }
            echo "<td valign='top' ><input name='amt2' size='5'/></td>";
            echo "<td valign='top'><input  name='netamt' class='netAmt' size='5'/></td>";
            echo "</tr>";
            }

            echo "</table>";
            echo "<br>";
            echo "<tr><td></td></tr>";
            echo "<center>";
            echo "<input type='submit' name='submit' value='Save' style='cursor:pointer;cursor:hand;'>&nbsp;&nbsp;&nbsp;&nbsp;";
            echo "<input type='reset' name='clear' value='Clear' style='cursor:pointer;cursor:hand;'>";
            echo "</center>";

         ?>
   </form>
 </body>
</html>

不知道这有什么问题,我们将不胜感激............

除此之外.....我可以在循环结束时得到这些减去值的总和,如Total:

1 个答案:

答案 0 :(得分:0)

您循环遍历每个input,但随后对每次丢弃的子进行计算。请尝试循环遍历每个tr

此外,您需要通过调用parseInt()来包装$('input[name=..]').val()(或者,如果是非整数,则需要等效)。使用代码,它将其值检索为字符串。尝试:

sumtable.find('tr').each(function() {
    var amt1 = parseInt($(this).find('input[name="amt1"]').val());
    var amt2 = parseInt($(this).find('input[name="amt2"]').val());
    sub = (amt1 - amt2)
    $(this).find('.netAmt').val(sub);
});