0
我已经创建了一个我可以动态添加的表格&删除产品。得到关于选择产品的所有产品的价格总和
就像我已经添加了三个产品$ 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);
}
});
});
});
我已经做了添加和删除,但我想快速求和每当我删除或添加的产品..
我希望你能得到它。
你的'sum'函数或者你已经有'sum'值的地方? – Pavlo
您可以在db中添加或删除数据后从db中计算总和并返回响应中的值。您可以在jquery success:函数中显示总和 –