0

我有一个 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 是:

4

1 回答 1

0

您正在遍历 every input,然后对每次都丢弃的 sub 进行计算。尝试循环遍历每个tr

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

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);
});
于 2013-07-03T12:18:58.303 回答