首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在选择产品时获取所有产品的价格总和

在选择产品时获取所有产品的价格总和
EN

Stack Overflow用户
提问于 2014-02-19 20:11:32
回答 1查看 217关注 0票数 0

我已经创建了一个表单,可以在其中动态添加和删除产品。

例如,我添加了三个产品,$10,$20,$30。

我临时将这些产品添加到数据库中。

现在我想要所有产品的最终总和,而不是刷新页面。

代码语言:javascript
复制
 $(document).ready(function() {

    //##### send add record Ajax request to response.php #########
    $("#FormSubmit").click(function (e) {


            e.preventDefault();
            if($("#fund_amount").val()==='')
            {
                alert("Please enter amount");
                return false;
            }
            var fund_amount = $("#fund_amount").val();
            var fund_category=$("input[name=fundcategory]:checked").val();

            var dataString = 'fund_amount='+ fund_amount + '&fund_category=' + fund_category;

            jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "response", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:dataString,

            success:function(response){
                $("#productholder").append(response);
                $("#fund_amount").val(''); //empty text field on successful
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
            });
    });

    //##### Send delete Ajax request to response.php #########
    $("body").on("click", "#productholder .del_button", function(e) {
         e.returnValue = false;
         var clickedID = this.id.split('-'); //Split string (Split works as PHP explode)
         var DbNumberID = clickedID[1]; //and get number from array
         var myData = 'recordToDelete='+ DbNumberID; //build a post data structure

            jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "response", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:myData, //Form variables
            success:function(response){
                //on success, hide  element user wants to delete.
                $('#item_'+DbNumberID).fadeOut("slow");
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
            });

    });

     });

我已经做了添加和删除,但每当我删除或添加产品时,我都想要快速求和。

我希望你能得到它。

EN

回答 1

Stack Overflow用户

发布于 2014-02-19 20:24:49

您可以发送一个ajax请求来获取购物车\temp DB中的产品总数。

使用像这样的东西

代码语言:javascript
复制
function get_product_sum()
{
     jQuery.ajax({
            type: "POST", // HTTP method POST or GET
            url: "response", //Where to make Ajax calls
            dataType:"text", // Data type, HTML, json etc.
            data:"sum_of_the_product", //sum request methord
            success:function(sumasresponse){

               alert(sumasresponse)
            },
            error:function (xhr, ajaxOptions, thrownError){
                //On error, we alert user
                alert(thrownError);
            }
            });
}

并在需要sum时调用get_product_sum (添加产品成功/删除产品成功)

注意:更合适的方式是在发生增/删操作时从DB中获取和,并将其与之一起返回,然后在增/删请求成功时将其分开

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21880112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档