我想在更改数量时计算总金额

时间:2016-12-05 12:48:08

标签: php jquery ajax

我希望在更改数量时获得总金额。

此脚本在while循环中不起作用:(quantity * amount = total_amount)

我该如何解决这个问题?请帮我解决这个问题。



function calculateTotal() {
   var totalAmt = document.addem.total.value;
   totalR = eval(totalAmt * document.addem.tb1.value);
		
   document.getElementById('total_amount').innerHTML = totalR;
}

<table>
    <tr>
        <td>Id</td>
        <td>Product</td>
        <td>Quantity</td>
        <td>Total</td>
    </tr>
    <tr>
        <?php
        $sq=mysql_query("select * from cart_sample where email='$ema'");
        while($row = mysql_fetch_array($sq))
        { ?>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['product']; ?></td>

            <form name="addem"  id="addem" />
                <td><input type="text" name="tb1" onkeyup="calculateTotal()" value="<?php echo $row['quantity']; ?>" /></td>
                <input type="hidden" name="total"  value="<?php echo $row['amount']; ?>" />
                <td><span id="total_amount"></span></td>
      <?php } ?>        </tr>
        </table>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

由于你正在使用jquery,你可以避免内联事件并为你的输入提供一般类,然后为所有输入创建一个事件:

<input type="text" class='quantity' name="tb1" onkeyup="calculateTotal()" value="<?php echo $row['quantity']; ?>" />
<input type="hidden" class='total' name="total"  value="<?php echo $row['amount']; ?>" />
<span class="total_amount"></span>

JS:

$('.quantity').on('input', function(){
    var form = $(this).closest('form');
    var totalAmt = parseInt(form.find('.total').val());
    var quantity = parseInt($(this).val());

    form.find('.total_amount').text(totalAmt*quantity);
})

注意:表单应位于td内。

希望这会有所帮助。

$('.quantity').on('input', function(){
  var form = $(this).closest('form');
  var totalAmt = parseInt(form.find('.total').val());
  var quantity = parseInt($(this).val());

  form.find('.total_amount').text(totalAmt*quantity);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" class='quantity' name="tb1" value="1" />
  <input type="hidden" class='total' name="total" value="10" />
  <span class="total_amount">10</span>
</form>

<form>
  <input type="text" class='quantity' name="tb1" value="2" />
  <input type="hidden" class='total' name="total" value="10" />
  <span class="total_amount">20</span>
</form>

<form>
  <input type="text" class='quantity' name="tb1" value="3" />
  <input type="hidden" class='total' name="total" value="10" />
  <span class="total_amount">30</span>
</form>

没有表格:

$('.quantity').on('input', function(){
  var parent = $(this).closest('tr');
  var totalAmt = parseInt(parent.find('.total').val());
  var quantity = parseInt($(this).val());

  parent.find('.total_amount').text(totalAmt*quantity);

  calcul_total_quatities();
})

function calcul_total_quatities()
{
  var total = 0;
  $('.total_amount').each(function(){
    total += parseInt( $(this).text() );
  })
  $('.total_all_amounts').text(total);

  post_data_to_server($('.total_amount').val(),total);
}


function post_data_to_server(total_amount,total_all_amounts)
{
  $.ajax({
    type: 'post',
    url: 'your_page.php',
    data: {
      total_amount: total_amount,
      total_all_amounts: total_all_amounts
    },
    success: function( data ) {
      //'data' represent the message back from the PHP page
      
      console.log( data );
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Id</td>
    <td>Product</td>
    <td>Quantity</td>
    <td>Total</td>
  </tr>
  <tr>
    <td>ID</td>
    <td>Product</td>
    <td>
      <input type="text" class='quantity' name="tb1" value="1" />
    </td>
    <td><input type="hidden" class='total' name="total" value="10" /></td>
    <td><span class="total_amount">10</span></td>
  </tr>
  <tr>
    <td>ID</td>
    <td>Product</td>
    <td>
      <input type="text" class='quantity' value="2" />
    </td>
    <td><input type="hidden" class='total' value="10" /></td>
    <td><span class="total_amount">20</span></td>
  </tr>
  <tr>
    <td>ID</td>
    <td>Product</td>
    <td>
      <input type="text" class='quantity' value="3" />
    </td>
    <td><input type="hidden" class='total' value="10" /></td>
    <td><span class="total_amount">30</span></td>
  </tr>
    <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><span class="total_all_amounts">60</span></td>
  </tr>
</table>

答案 1 :(得分:0)

使用jquery只需在页面顶部添加脚本标记

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

位于</body>

之前的页面底部
<script type="text/javascript">

    function calculateTotal() {

        var totalAmount = 0;
        $('#addem').find('input').each(function(){
            totalAmount += parseInt($(this).val()); //convert to integer so sum can be done correctly.
        });
        $('#total_amount').html(totalAmount);   
    }

</script>