我已经创建了一个表单,可以在其中动态添加和删除产品。
例如,我添加了三个产品,$10,$20,$30。
我临时将这些产品添加到数据库中。
现在我想要所有产品的最终总和,而不是刷新页面。
$(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);
}
});
});
});我已经做了添加和删除,但每当我删除或添加产品时,我都想要快速求和。
我希望你能得到它。
发布于 2014-02-19 20:24:49
您可以发送一个ajax请求来获取购物车\temp DB中的产品总数。
使用像这样的东西
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中获取和,并将其与之一起返回,然后在增/删请求成功时将其分开
https://stackoverflow.com/questions/21880112
复制相似问题