这对我来说是一个非常复杂的问题,我有产品表和相关产品表,我可以插入产品和相关产品,但我必须点击两个按钮,这不是一种合乎逻辑的方式,我有显示产品的模态对话和相关产品的复选框,我想将两个ajax组合在一个按钮中,我尝试了很多方法,但没有使用
ajax用于相关产品
$(function(){
$('.addrelated').on("click", function (){
var insert=[];
$('.get_value').each(function(){
if($(this).is(":checked"))
{
insert.push($(this).attr('data-id'));
}
});
insert=insert.toString();
$.ajax({
url:"add-to-cart",
method:"get",
data:{insert:insert},
success:function(data){
}
});
});
});
主产品的ajax
$(function(){
$('.add_to_cart').on("click", function () {
var id = ($("#item_id").val());
$quantity=$('#quantity').text();
$.ajax({
url: 'cart/' + id+'/edit',
data: {'quantity':$quantity},
type: "get",
success: function (data)
{
$('#myModal').modal('hide');
$('#cart_product').html(data);
}
}); }); });
在第一次ajax之后使用这个
$.ajax({
url:"add-to-cart",
method:"get",
data:{insert:insert},
success:function(data){
}
}).always(function () {// here the function for the main product
我不太清楚你的问题,但看起来你可以这样做:
$(function(){
$('.addboth').on("click", function (){
// related product code starts here
var insert=[];
$('.get_value').each(function(){
if($(this).is(":checked")) {
insert.push($(this).attr('data-id'));
}
});
insert=insert.toString();
$.ajax({
url:"add-to-cart",
method:"get",
data:{insert:insert},
success:function(data){
}
});
// main product code starts here
var id = ($("#item_id").val());
$quantity=$('#quantity').text();
$.ajax({
url: 'cart/' + id+'/edit',
data: {'quantity':$quantity},
type: "get",
success: function (data) {
$('#myModal').modal('hide');
$('#cart_product').html(data);
}
});
});