使用表格单元格值进行计算

时间:2015-02-06 16:05:08

标签: jquery mysql sql html-table

我在这个问题上已经挣扎了很长时间。 用Google搜索并搜索了Stack Overflow上的一些代码,但是在完成它时并没有太多的快乐。

我有一个表,它是从PHP / SQL填充的。

它将具有:

成本qty1 qty2 qty3 totalqty rowcost

我需要总数qty1,qty2,qty3,而rowcost将是cost * totalqty

这是我到目前为止添加行的jQuery,但它似乎不起作用:

    <script>
function findTotals() {
    $("tbody tr").each(function() {
        row_total = 0; 
        $("td:not(.total) input:number",this).each(function() {
           row_total += Number($(this).val());
        }); 
        $(".total :input:number",this).val(row_total);
    });

}</script>

以下是其中一行的PHP(可能有点乱,但我不是专家):

<?php
while($row = mysql_fetch_array($result))
  {
  echo "<tr class=\"" . $row['prod_type'] . " " . $row['prod_cat'] . " " . $row['prod_state'] . " liquidrow\">
        <th style=\"display:none\">" . $row['prod_cat']  . "</th>
        <th style=\"display:none\" class=\"prod_cost price\">" . $row['prod_cost']  . "</th>
        <th style=\"display:none\">" . $row['prod_rrp']  . "</th>
        <th class=\"liquidtr\">" . $row['prod_name']  . "<br /><span class=\"flavourname\"></span></th>

        <td>";      
        if ($row["boption00"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control\" name=\"orderdetails[" . $row['prod_selectname'] . 00 . "]\" id=\"" . $row['prod_selectname'] . 00 . "\">"; 

  echo "</td>
        <td>"; 

        if ($row["boption03"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control\" name=\"orderdetails[" . $row['prod_selectname'] . 03 . "]\" id=\"" . $row['prod_selectname'] . 03 . "\">"; 

  echo "</td>
        <td>"; 

        if ($row["boption06"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control\" name=\"orderdetails[" . $row['prod_selectname'] . 06 . "]\" id=\"" . $row['prod_selectname'] . 06 . "\">"; 

  echo "</td>
        <td>"; 
        if ($row["boption11"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control\" name=\"orderdetails[" . $row['prod_selectname'] . 11 . "]\" id=\"" . $row['prod_selectname'] . 11 . "\">"; 

  echo "</td>
        <td>"; 

        if ($row["boption12"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control\" name=\"orderdetails[" . $row['prod_selectname'] . 12 . "]\" id=\"" . $row['prod_selectname'] . 12 . "\">"; 

  echo "</td>
        <td>"; 

        if ($row["boption18"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control\" name=\"orderdetails[" . $row['prod_selectname'] . 18 . "]\" id=\"" . $row['prod_selectname'] . 18 . "\">"; 

  echo "</td>
        <td>"; 

        if ($row["boptiont"] == 1) 
  echo "<input tabindex=\"-1\" class=\"form-control\" type=\"checkbox\" name=\"orderdetails[" . $row['prod_selectname'] . t . "]\" id=\"" . $row['prod_selectname'] . t . "\" value=\"1\"></select>"; 

  echo "</td>
        <td>"; 

        if ($row["boption24"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control\" name=\"orderdetails[" . $row['prod_selectname'] . 24 . "]\" id=\"" . $row['prod_selectname'] . 24 . "\">"; 

  echo "</td>
        <td>"; 

        if ($row["boption36"] == 1) 
  echo "<input type=\"number\" min=\"5\" step=\"5\" class=\"form-control quantity\" name=\"orderdetails[" . $row['prod_selectname'] . 36 . "]\" id=\"" . $row['prod_selectname'] . 36 . "\">"; 

  echo "</td>
        <td style=\"padding:5px; color:#F00; font-size:12px\" class=\"" . $row['prod_selectname'] . 'total rowtotal' . "\"></td><td class=\"row_total\"><input tabindex=\"-1\" class=\"form-control\" type=\"text\" readonly=\"readonly\" /></td>"; 

if ($_SESSION['userlevel'] == "9") {  echo 

'<td style="padding:5px 10px" class="prodedit"><a href="liquid_edit.php?prod_id=' . $row['prod_id'] . '"><i class="fa fa-pencil-square-o fa-2x" title="Edit Product"></i></a><a class=\"delete\" href="liquid_delete.php?prod_id=' . $row['prod_id'] . '"><i class="fa fa-trash-o fa-2x" title="Delete Product"></i></a></td>'
;}
  echo "</tr>";
  }; // Display all liquids from database

  ?>

感谢任何帮助。

0 个答案:

没有答案