weixin_33726943 2015-12-07 06:26 采纳率: 0%
浏览 58

jQuery Ajax乘法

I need your help please! I want to multiply the quantity with the single price and put it in id called total using Ajax( I don't know Ajax well)

$(function () {
    "use strict";
    $('.qty').on('keyup', function () {
        var singl_price = $(".single_price").val(),
            qty = $(".qty").val(),
            total_price = qty * singl_price;
            $("#total").text(total_price);

        $.ajax({
            url: 'cart.php',
            type: 'POST',
            data: '',
            success: function (data) {
                if (data === "ok") {                    
                }
            },
            error: function (e) {
            }
        });    
    });   
});
<form action="cart.php" method="post" >
  <input type="text" name="qty" class="qty" value="<?php echo $_POST['qty']; ?>" />
</form>

<div class="single_price"> 20 </div>

<div id="total"> </div>

</div>
  • 写回答

2条回答 默认 最新

  • python小菜 2015-12-07 06:30
    关注

    try:

    $('.qty').on('keyup', function () {
            $.ajax({
                url: 'cart.php',
                type: 'POST',
                data: {total_price:total_price},
                success: function (data) {
                       var total = 0;
                       $('.qty').each(function(i,v){
                       var singl_price = $(v).next(".single_price").text();
                       var qty = $(v).val().length > 0 ?$(v).val():0;
    
                      total +=  parseInt(qty) * parseInt(singl_price);
                      });
                      $('#total').text(total);
    
                    if (data === "ok") {                    
                    }
                },
                error: function (e) {
                }
            });    
        });   
    

    html:

    <form action="cart.php" method="post" >
          <input type="text" name="qty" class="qty" value="<?php echo $_POST['qty']; ?>" />
        <div class="single_price"> 20 </div>
        </form>
    
    
    
    <div id="total">0</div>
    

    https://jsfiddle.net/o7hb7s6g/2/

    Note:use parseFloat instead of parseInt if you have price with like 3.99

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向