2011-12-18 60 views
2

我在我的表計算每個錶行值自動

成本有這樣的數據   郵資   利潤   貝寶   售價
2.75                                                                         16.75
5.75                                                                         19.75
3.99                                                                         17.99

值花費表正從數據庫並在郵資值,利潤取,和paypal都是不變的。當我更新數據庫中的成本值時,它現在將顯示在表中,並根據我更新的數據進行自動計算,並在selling_price列中顯示答案。我對如何在jQuery中實現客戶端計算有點困惑。任何人都可以幫我解決這個問題。任何幫助將得到更多的讚賞。

這裏是我創建

<html> 
<head> 
    <script language="javascript" src="jquery.js"></script> 

    <script> 
     $(document).ready(function() { 
      //iterate through each textboxes and add keyup handler to trigger sum event 
      $(".txt").each(function() 
      { 
       $(this).keyup(function() { 
        calculateSellingPrice(); 
       }); 
      }); 
     }); 

     function calculateSellingPrice() { 
      var sellingprice = 0; 
      //iterate through each textboxes and add the values 
      $(".txt").each(function() { 
       //add only if the value is number 
       if (!isNaN(this.value) && this.value.length != 0) { 
        sellingprice += parseFloat(this.value); 
        $(this).css("background-color", "#FEFFB0"); 
       } 
       else if (this.value.length != 0){ 
       $(this).css("background-color", "red"); 
       } 
      }); 
      $("#sellingprice").html(sellingprice.toFixed(2)); 
     } 
    </script> 
</head> 
<body> 

     <table border = "0">  
      <tr bgcolor="#cccccc"> 
       <td><center>Selling Price</center></td> 
       <td><center>Cost</center></td> 
       <td><center>Postage</center></td> 
       <td><center>Profit</center></td> 
       <td><center>Paypal</center></td> 
      </tr> 

      <?php 

      $result = mysql_query("SELECT cost FROM table"); 
      while ($myrow = mysql_fetch_row($result)) 
      { 
      ?> 
       <tr> 
        <td> 
         <?php echo "<input type='text' id='sellingprice' name='sellingprice' size='10' />"; ?> 
        </td> 
        <td> 
         <?php echo "<input class='txt' type='text' id='cost' name='cost' size='10' value='$myrow[1]' />"; ?> 
        </td> 
        <td> 
         <?php echo "<input class='txt' type='text' id='postage' name='postage' size='10' value='3' />"; ?> 
        </td> 
        <td> 
         <?php echo "<input class='txt' type='text' id='profit' name='profit' size='10' value='6' />"; ?> 
        </td> 
        <td> 
         <?php echo "<input class='txt' type='text' id='paypal' name='paypal' size='10' value='5' />"; ?> 
        </td> 
       </tr> 
       <?php 
      } 
       ?> 
     </table> 

</body> 
</html> 

我剛剛找到在互聯網上的jquery代碼,雖然我瞭解一點jQuery代碼,但我的股票,無法繼續的代碼。

+0

在格式化代碼和空格分隔的co時,請注意編輯器中的「{}」按鈕ntent。您不需要' '和'
'來完成此操作。另外,標記在哪裏? – 2011-12-18 03:57:28

+0

需要多長時間才能完成所有這些' '? – 2011-12-18 04:00:56

+0

這就像你要找的東西? http://jsfiddle.net/jxuvT/ – 2011-12-18 04:07:35

回答

-1

看看這篇文章可以看出U如何從表中的行或列 http://www.amitpatil.me/table-manipulation-with-jquery/

+0

我可以根據您的示例表先生來計算嗎? – jovazel 2011-12-18 04:30:08

+0

是的,你可以...只需使用parseInt()函數將字符串值轉換爲整數值 – 2011-12-18 09:47:06

0

每個值,您需要遍歷和獨立計算每一行。 在你的例子中,jQuery一次選擇每個單元格輸入,然後計算是否更新所有「#sellingprice」輸入的顯示。雖然因爲「#sellingprice」需要通過.val()而不是.html()來更新,所以不會出現任何顯示變化。這更接近您所需要的。

$(document).ready(function(){ 
    $('tr').each(function(){ 
     var total = 0; 
     $(this).find('.txt:not(:first)').each(function(){ 
      total += (+$(this).val()); 
     }); 
     $(this).find('.txt:first').val(total).css("background-color", "#FEFFB0"); 
    }); 
}); 
  • 循環通過每個TR:$( 'TR')每個(functi ...
  • 組或,重置計數器:VAR總= 0;
  • 通過所有輸入環路(不包括第一個總數):$(this).find('。txt:not(:first)')...
  • 設置新總數:$(this).find('。txt:first')。 val(總數)...
+0

是的基於我的代碼它只是計算所有的單元格值。我會嘗試使用此更新我的代碼。 – jovazel 2011-12-18 04:32:34

+0

太好了。您可能需要稍作調整才能使其正常工作 - 只需更新即可顯示每行的內容 – 2011-12-18 04:34:26

+0

我嘗試了您的代碼,它解決了一些我正在尋找的問題,謝謝@crolpa .... – jovazel 2011-12-18 04:45:56