jQuery中的Nan以查看多个行值

第六星

当我显示单行值((数量*产品)=总计)时,它可以工作。

在此处输入图片说明

但是使用会话在一行中添加更多值会导致在我更改数量值时显示类似NaN的错误。

在此处输入图片说明

我的代码:

<table class="table">
<thead>
<tr>
    <th class="pro-remove">Remove</th>
    <th class="pro-thumbnail">Image</th>
    <th class="pro-title">Product</th>
    <th class="pro-price">Price</th>
    <th class="pro-quantity">Quantity</th> 
    <th class="pro-subtotal"> Total</th>
</tr>
</thead>
<tbody> 
<?php 
    $cartid = $_GET['proid'];  
    $sqlpd = mysql_query("select * from orderdetail LEFT JOIN products ON orderdetail.product_id = products.product_id where sessionid='$session_id'")or die(mysql_error()); 
    //echo "select * from orderdetail LEFT JOIN products ON orderdetail.product_id = products.product_id where sessionid='$session_id'";exit();
    while($getpd = mysql_fetch_array($sqlpd)){
            $product_id = $getpd['product_id'];
        ?> 
<tr>
    <td class="pro-remove"><a href="javascript:void(0);"  name="cart_remove" id="cart_remove"><i class="fa fa-trash-o"></i></a></td>
    <td class="pro-thumbnail"> 
        <img src="admin/<?php echo $getpd['medium_image'];?>" height="50" width="50">
    </td> 
    <td class="pro-title"><?php echo $getpd['product_id'];?></td>
    <td class="pro-price"><span class="price"><i class="fa fa-inr"></i><?php echo number_format($getpd['actual_price'],2);?>
</span></td>

    <td class="pro-quantity">
        <input type="number" class="cardqty-1" id="quantity" min="1" value="1" name="quantity" style="width:50px;">
        <a href="javascript:void();" class="updatecart" data-id="1" data-prd_id="380" data-rowid="c2b5d53083a7597dd75ac36d9fd93581"><i class="fa fa-refresh" aria-hidden="true"></i></a>
    </td>
    <td class="pro-subtotal"><span class="total"><i class="fa fa-inr"></i><?php echo number_format($getpd['actual_price'],2);?></span></td>
</tr>
<?php }?>
</tbody>
<tfoot>
<tr>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th >Sub Total</th>
    <th class="pro-subtotal text-center"><span class="total"><i class="fa fa-inr"></i><?php echo $tot_amt += $getpd['actual_price'];?></span></th>

</tr>
<tr>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th >GST(12%)</th>
    <th class="pro-subtotal text-center"><i class="fa fa-inr"></i><?php echo  $tot_amt +=$getpd['commission_amount'];?></th>
</tr>
<tr>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th >Grand Total</th>
    <th class="pro-subtotal text-center"><i class="fa fa-inr"></i><?php echo number_format($getpd['actual_price'],2);?></th>
</tr>
</tfoot>

我的Jquery脚本:

    <script type="text/javascript">
    $(document).ready(function() { 
        $('input[name=\'quantity\']').on('change keyup click', function() {
          var price = $('.price').text();
          var currency = $('.price').text();
          var quantity = $('#quantity').val(); 
          $('.total').text((price * quantity).toFixed(2));  
        });
    });
    </script>

HTML代码:

<table class="table">
    <thead>
        <tr>
            <th class="pro-remove">Remove</th>
            <th class="pro-thumbnail">Image</th>
            <th class="pro-title">Product</th>
            <th class="pro-price">Price</th>
            <th class="pro-quantity">Quantity</th> 
            <th class="pro-subtotal"> Total</th>
        </tr>
    </thead>
    <tbody> 

        <tr>
            <td class="pro-remove"><a href="javascript:void(0);"  name="cart_remove" id="cart_remove" class="delete"><i class="fa fa-trash-o"></i></a></td>
            <td class="pro-thumbnail"> 
                <img src="admin/assets/images/uploads/products/920535865hinite_caps.png" height="50" width="50">
            </td> 
            <td class="pro-title">1</td>
            <td class="pro-price"><span class="price"><i class="fa fa-inr"></i>360.00                                            </span></td>

            <td class="pro-quantity">
                <input type="number" class="cardqty-1 quantity" id="quantity" min="1" value="1" name="quantity" style="width:50px;">
                <a href="javascript:void();" class="updatecart" data-id="1" data-prd_id="380" data-rowid="c2b5d53083a7597dd75ac36d9fd93581"><i class="fa fa-refresh" aria-hidden="true"></i></a>
            </td>
            <td class="pro-subtotal"><span class="total"><i class="fa fa-inr"></i>360.00</span></td>
        </tr>

        <tr>
            <td class="pro-remove"><a href="javascript:void(0);"  name="cart_remove" id="cart_remove" class="delete"><i class="fa fa-trash-o"></i></a></td>
            <td class="pro-thumbnail"> 
                <img src="admin/assets/images/uploads/products/920535865hinite_caps.png" height="50" width="50">
            </td> 
            <td class="pro-title">1</td>
            <td class="pro-price"><span class="price"><i class="fa fa-inr"></i>360.00                                            </span></td>

            <td class="pro-quantity">
                <input type="number" class="cardqty-1 quantity" id="quantity" min="1" value="1" name="quantity" style="width:50px;">
                <a href="javascript:void();" class="updatecart" data-id="1" data-prd_id="380" data-rowid="c2b5d53083a7597dd75ac36d9fd93581"><i class="fa fa-refresh" aria-hidden="true"></i></a>
            </td>
            <td class="pro-subtotal"><span class="total"><i class="fa fa-inr"></i>360.00</span></td>
        </tr>
                                                    </tbody>
    <tfoot>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th >Sub Total</th>
            <th class="pro-subtotal text-center"><span class="total"><i class="fa fa-inr"></i>0</span></th>

        </tr>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th >GST(12%)</th>
            <th class="pro-subtotal text-center"><i class="fa fa-inr"></i>0</th>
        </tr>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th >Grand Total</th>
            <th class="pro-subtotal text-center"><i class="fa fa-inr"></i>0.00</th>
        </tr>
    </tfoot>
</table>
德夫西·奥德拉

您可以使用类并以相同方式计算最终总计

$(document).ready(function() { 

  $(document).on('change keyup click', '.quantity', function(e){
  
    var _t = $(this);
    var  price = parseFloat(_t.parents('tr').find('.price').text());
    var  currency = _t.parents('tr').find('.price').text();
    var  quantity = parseInt(_t.parents('tr').find('.quantity').val());
    
     _t.parents('tr').find('.ttotal').text((price * quantity).toFixed(2));
     
     var subtotal = 0;
    $('.ttotal').each(function(){
        var _te = $(this);
         var oneval = _te.html().replace('<i class="fa fa-inr"></i>','');
        subtotal += parseFloat(oneval);
    });
     $('.last_subtotal').text(subtotal);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
    <thead>
        <tr>
            <th class="pro-remove">Remove</th>
            <th class="pro-thumbnail">Image</th>
            <th class="pro-title">Product</th>
            <th class="pro-price">Price</th>
            <th class="pro-quantity">Quantity</th> 
            <th class="pro-subtotal"> Total</th>
        </tr>
    </thead>
    <tbody> 

        <tr>
            <td class="pro-remove"><a href="javascript:void(0);"  name="cart_remove" id="cart_remove" class="delete"><i class="fa fa-trash-o"></i></a></td>
            <td class="pro-thumbnail"> 
                <img src="admin/assets/images/uploads/products/920535865hinite_caps.png" height="50" width="50">
            </td> 
            <td class="pro-title">1</td>
            <td class="pro-price"><span class="price"><i class="fa fa-inr"></i>360.00                                            </span></td>

            <td class="pro-quantity">
                <input type="number" class="cardqty-1 quantity" id="quantity" min="1" value="1" name="quantity" style="width:50px;">
                <a href="javascript:void();" class="updatecart" data-id="1" data-prd_id="380" data-rowid="c2b5d53083a7597dd75ac36d9fd93581"><i class="fa fa-refresh" aria-hidden="true"></i></a>
            </td>
            <td class="pro-subtotal"><span class="total ttotal"><i class="fa fa-inr"></i>360.00</span></td>
        </tr>

        <tr>
            <td class="pro-remove"><a href="javascript:void(0);"  name="cart_remove" id="cart_remove" class="delete"><i class="fa fa-trash-o"></i></a></td>
            <td class="pro-thumbnail"> 
                <img src="admin/assets/images/uploads/products/920535865hinite_caps.png" height="50" width="50">
            </td> 
            <td class="pro-title">1</td>
            <td class="pro-price"><span class="price"><i class="fa fa-inr"></i>360.00                                            </span></td>

            <td class="pro-quantity">
                <input type="number" class="cardqty-1 quantity" id="quantity" min="1" value="1" name="quantity" style="width:50px;">
                <a href="javascript:void();" class="updatecart" data-id="1" data-prd_id="380" data-rowid="c2b5d53083a7597dd75ac36d9fd93581"><i class="fa fa-refresh" aria-hidden="true"></i></a>
            </td>
            <td class="pro-subtotal"><span class="total ttotal"><i class="fa fa-inr"></i>360.00</span></td>
        </tr>
                                                    </tbody>
    <tfoot>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th >Sub Total</th>
            <th class="pro-subtotal text-center"><span class="total last_subtotal"><i class="fa fa-inr"></i>0</span></th>

        </tr>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th >GST(12%)</th>
            <th class="pro-subtotal text-center"><i class="fa fa-inr"></i>0</th>
        </tr>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th >Grand Total</th>
            <th class="pro-subtotal text-center"><i class="fa fa-inr"></i>0.00</th>
        </tr>
    </tfoot>
</table>

旧答案您应该使用Number进行操作,因此请使用parseIntparseFloat

$(document).ready(function() { 
        $('input[name=\'quantity\']').on('change keyup click', function() {
          var price = parseFloat($('.price').text());
          var currency = $('.price').text();
          var quantity = parseInt($('#quantity').val()); 
          $('.total').text((price * quantity).toFixed(2));  
        });
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Excel:比较多个列以查看是否小于所有行的另一列中的值

来自分类Dev

在jQuery中获取NaN而不是id值

来自分类Dev

向后插值Pandas / Python中的多个连续nan?

来自分类Dev

如何比较 Python 中的行以查看值的差异?

来自分类Dev

jQuery在输入标签中推送多个值

来自分类Dev

在单个SQL行中存储多个值

来自分类Dev

在一行中显示多个值

来自分类Dev

基于多个条件的列中的值到行中的值

来自分类Dev

如何用单个NaN替换熊猫数据框中的多个NaN行

来自分类Dev

大量的NaN和不正确的值在jQuery块中?

来自分类Dev

从累积的行值进行平均计算,而忽略MATLAB中的NaN

来自分类Dev

NaN值试图过滤熊猫数据框中的行

来自分类Dev

选择行中具有最少NaN值的矩阵的列

来自分类Dev

如何在Java中的Same HashMap中查看多个键之间的公共值?

来自分类Dev

使用jQuery从多个表中删除行

来自分类Dev

从行中的多个值计算一个值

来自分类Dev

选择希望列中的值具有多个值的行

来自分类Dev

Grep 用于行中的多个值并仅输出 MULTIPLE 值

来自分类Dev

如果Nan位于单独的行中,请用Nan替换值-熊猫

来自分类Dev

查看值是否存在于多个数组中,如果存在则更改一个数组中的值

来自分类Dev

在 Bazel 中查看配置值

来自分类Dev

如何使用php和mysql将创建的行值上的多个jquery插入数据库中?

来自分类Dev

jQuery函数导致NaN值

来自分类Dev

如何在jQuery中在多个div值中查找div?

来自分类Dev

jQuery获取表中的行值

来自分类Dev

jQuery获取表中的行值

来自分类Dev

查看熊猫中的重复行

来自分类Dev

如果一个值是 NaN,Pandas 用 NaN 替换一行中的所有值

来自分类Dev

jQuery的多个隐藏值

Related 相关文章

热门标签

归档