Javascript 当产品出现在购物车中时,没有弹出窗口
将产品添加到购物车时,我使用的代码如下:Javascript 当产品出现在购物车中时,没有弹出窗口,javascript,php,json,laravel,Javascript,Php,Json,Laravel,将产品添加到购物车时,我使用的代码如下: addproduct(itemId) { this.showLoading = true this.$http.post('/shampoo', {'item': itemId}).then((response) => { swal({ title: "Success!", text: "Product added to your basket!",
addproduct(itemId) {
this.showLoading = true
this.$http.post('/shampoo', {'item': itemId}).then((response) => {
swal({
title: "Success!",
text: "Product added to your basket!",
type: "success",
timer: 1500,
showConfirmButton: false
})
this.showLoading = false
}, (response) => {
this.showLoading = false
})
我创建了一个弹出式表单,在购物车上没有任何产品,一旦他们点击添加到购物车按钮,它就会弹出,一旦产品在购物车上,如果有人需要添加另一个产品,它就会再次显示弹出窗口。如果产品在购物车上,我不希望弹出窗口
这是弹出表单的代码
show() {
this.showLoading = true;
$(".loading").css('display', 'block');
$('#products').modal("hide");
//
let code = $('input[name=code]').val();
if(code) {
this.$http.post('/codee', { code: code}).then((response) => {
// $('#basic-form').submit();
}, (response) => {
swal({
title: "Error!",
text: "Sorry this Code is invalid!",
type: "error",
timer: 2500,
showConfirmButton: false
})
$(".loading").css('display', 'none');
this.showLoading = false
$('input[name=code]').val('')
})
单击服务器端的“添加到购物车”时,需要检查商品id是否在购物车中,并返回一个标志以指示商品是否在cat中。我认为这段代码是弹出窗口的原因
swal({
title: "Error!",
text: "Sorry this Code is invalid!",
type: "error",
timer: 2500,
showConfirmButton: false
})
您需要检查从服务器返回的值,如果它返回一个指示该项已经存在的标志,只需返回false;这将阻止访问弹出代码。或者您可以将pop代码包装在if语句中。e、 g
if(response.flag === 1){
return false;
// your pop up code goes here
}
or
if(response.flag === 0){
// your pop up code goes here
}
// flag can assume 0 => Added but does not exist, 1=> added but already
// exist. ect
// while the actual data will be stored in response.data
但我想知道你为什么要实施这个计划。如果用户单击“添加到购物车”,则如果购物车中已存在相同项目,则应增加该项目的数量