JQuery 从每个单元格获取商品总价显示它,工作正常,但不正确

发布于 2024-11-28 14:44:23 字数 2984 浏览 0 评论 0原文

我有这个 javascript、jquery 函数,(如下)

  1. 它获取表格的每个表格单元格(class=“total_item_price”)内的文本。
  2. 它将其放入一个数组(prices_array)中,
  3. 将prices_array 相加并将其格式化为小数点后两位。
  4. 然后将其输出到total_order_price中,或者如果do_request设置则返回它。

问题:我有一个函数,可以从购物篮中删除商品,然后调用此函数 (getTotalPrice) 来更新价格字段。该部件无法正常工作,并且无法产生正确的价格。

基本上,我需要这个函数来:

  • 获取单元格内的 (.total_item_price) 的价格。
  • 获取运费 (.shipping_price) + 将它们全部添加到
  • 单元格内显示 (.total_order_price)

然后当我调用删除函数时,我可以调用 ^this^ 函数以希望正确更新价格。
我还在 DOM Ready 上调用此 getTotalPrice 函数来更新价格,因此它正常工作很重要,当我调用删除函数(如下)时,它也必须工作。

我有一个 jsfiddle 但它不起作用, 但这段代码确实可以在我的本地主机上运行。我必须为 jsfiddle 压缩它,它在某个地方坏了。请随意编辑您想要的方式。


Here Is the Code(!): This function gets the total price and displays it.

function getTotalPrice(do_request)
{
var prices_array = new Array(); // Where our prices are held 

// For each .total_item_price - a <td> within my table.
$(".total_item_price").each(function(e){
    var text = $(this).text();                   // Get the value
    var prices = text.substring(1, text.length); // Format it into a string
    prices_array.push(prices);                   // Push it onto our array
});

var result = eval(0);
//  Add up our array
for(i = 0; i < prices_array.length; i++)
{
    temp = eval(prices_array[i]);
    result += temp;
}
// Round up our result to 2 Decimal Places
result = Math.round(result*100)/100;
// Output/Return our result
if (do_request == null)
{
    // We want to add our shipping Price and Display the total
    // Get the Shipping Price
    var shipping_price = $(".shipping_price").html();
    shipping_price = shipping_price.substring(1, shipping_price.length);

    // Add em
    result += eval(shipping_price);

    // Round our result to 2 decimal places
    var result=Math.round(result*100)/100;

    // Update & Display the Result
    $('.total_order_price').html("<b>£" + result + "</b>");        
}
else 
{
    // Otherwise we just want the total price and return it.
    return result;
}
}

这是我用来从表中删除一行并更新价格的函数。

// Delete Item from Basket, Run on click of delete button
function delete_item(e)
{
doIt = confirm('Delete Item from Basket?\r\n You can not undo this action.');
if(doIt)
{
  // Get our basket
  var basket_md5 = $(e).parent().find("input[name='basket_md5']").val();
  var url = "<?php echo SERVER_URL."shop/basket"; ?>";
  // Post to basket
  $.post(url, { "do": "delete_item","basket_md5": basket_md5});

  // Delete Row from Table
  // Row Scope
  var row = $(e).parent().parent();

  // Effect & Remove from DOM
  $(row).fadeOut(1000, function(){ 
      $(this).remove();

  });

  // Update the Prices (again)
  //tprice = getTotalPrice("return");
  //$('.total_order_price').html("<b>£" + tprice + "</b>"); 
  getTotalPrice();
 }
} 

I have this javascript, jquery function, (below)

  1. It gets the text inside each table cell (of class="total_item_price") of a table.
  2. It puts it into an array (prices_array)
  3. Adds up prices_array and formats them to 2 Decimal Places.
  4. Then outputs it into the total_order_price, or returns it if do_request isset.

Problem: I have a function that deletes a item from the basket, then calls this function (getTotalPrice) to update the prices field. This part does not work correctly, and is not producing the correct price.

Basically, I need this function, to:

  • Get the price(s) of (.total_item_price) which is inside a cell.
  • Get the shipping price (.shipping_price) + Add them all up
  • Display it inside cell (.total_order_price)

Then When I call my delete function, I can call ^this^ function to hopefully update the price correctly.
I also call this getTotalPrice function on DOM Ready to update the prices, so its important it works correctly, It also has to work when I call my delete function (below).

I have a jsfiddle but it doesn't work, but this code does work on my localhost. I had to compact it for jsfiddle, and its broken somewhere. Feel free to edit how you want.


Here Is the Code(!):
This function gets the total price and displays it.

function getTotalPrice(do_request)
{
var prices_array = new Array(); // Where our prices are held 

// For each .total_item_price - a <td> within my table.
$(".total_item_price").each(function(e){
    var text = $(this).text();                   // Get the value
    var prices = text.substring(1, text.length); // Format it into a string
    prices_array.push(prices);                   // Push it onto our array
});

var result = eval(0);
//  Add up our array
for(i = 0; i < prices_array.length; i++)
{
    temp = eval(prices_array[i]);
    result += temp;
}
// Round up our result to 2 Decimal Places
result = Math.round(result*100)/100;
// Output/Return our result
if (do_request == null)
{
    // We want to add our shipping Price and Display the total
    // Get the Shipping Price
    var shipping_price = $(".shipping_price").html();
    shipping_price = shipping_price.substring(1, shipping_price.length);

    // Add em
    result += eval(shipping_price);

    // Round our result to 2 decimal places
    var result=Math.round(result*100)/100;

    // Update & Display the Result
    $('.total_order_price').html("<b>£" + result + "</b>");        
}
else 
{
    // Otherwise we just want the total price and return it.
    return result;
}
}

This is the function I made to delete a row from the table and update the prices.

// Delete Item from Basket, Run on click of delete button
function delete_item(e)
{
doIt = confirm('Delete Item from Basket?\r\n You can not undo this action.');
if(doIt)
{
  // Get our basket
  var basket_md5 = $(e).parent().find("input[name='basket_md5']").val();
  var url = "<?php echo SERVER_URL."shop/basket"; ?>";
  // Post to basket
  $.post(url, { "do": "delete_item","basket_md5": basket_md5});

  // Delete Row from Table
  // Row Scope
  var row = $(e).parent().parent();

  // Effect & Remove from DOM
  $(row).fadeOut(1000, function(){ 
      $(this).remove();

  });

  // Update the Prices (again)
  //tprice = getTotalPrice("return");
  //$('.total_order_price').html("<b>£" + tprice + "</b>"); 
  getTotalPrice();
 }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2024-12-05 14:44:23

函数delete_item(e)中移动'getTotalPrice();这里:

$(row).fadeOut(1000, function(){ 
      $(this).remove();
      // Update the Prices (again)
      //tprice = getTotalPrice("return");
     //$('.total_order_price').html("<b>£" + tprice + "</b>"); 
     getTotalPrice();
  });

In function delete_item(e) move 'getTotalPrice(); here:

$(row).fadeOut(1000, function(){ 
      $(this).remove();
      // Update the Prices (again)
      //tprice = getTotalPrice("return");
     //$('.total_order_price').html("<b>£" + tprice + "</b>"); 
     getTotalPrice();
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文