Javascript 功能未正确执行
Javascript function not getting executed properly
我正在调用 .getJSON
在 shopify 中执行一些 AJAX 脚本。回调函数有多个语句,但只有第一行在我调试时得到 executed.In 下面的代码,控件转到第
行
jQuery.getJSON('/cart.js', function(cart){
之后是显示购物车计数正确的警告框。但是,警报消息下方的代码未被执行。我哪里错了。
function addtocart(prod_handle, variant_id) {
var found = false;
var x = 0;
jQuery.getJSON('/cart.js', function(cart) {
alert(cart.item_count);
x = cart.item_count;
if (x > 0) {
jQuery.getJSON('/product/' + prod_handle + '.js', function(product) {
var selected_prod_tags = products.tags;
for (var index = 0; index < selected_prod_tags.length; index++) {
if (selected_prod_tags[index] == 'jamshedpur' || selected_prod_tags[index] == 'ranchi') {
var city = selected_prod_tags[index];
}
}
jQuery.getJSON('/product/' + cart.items[0].handle + '.js', function(product) {
var cart_prod_tags = products.tags;
for (index = 0; index < cart_prod_tags.length; index++) {
if (cart_prod_tags[index] == city) {
found = true;
jQuery.post('/cart/add.js', {
quantity: 1,
id: variant_id
});
}
}
if (found == false) {
alert("Error!! Please select products from same city");
}
});
});
} else {
jQuery.post('/cart/add.js', {
quantity: 1,
id: variant_id
});
}
});
}
您 运行 在接下来的两个请求中未定义产品。这只是一个错字吗?
请记住,getJSON 要求它是有效的 JSON 否则它会无提示地失败,具体取决于您拥有的 jQuery 版本。
jQuery.getJSON('/product/' + prod_handle + '.js', function(product) {
var selected_prod_tags = products.tags;
您可能还想考虑将部分代码抽象出来以分离函数,例如add.js 电话。
我正在调用 .getJSON
在 shopify 中执行一些 AJAX 脚本。回调函数有多个语句,但只有第一行在我调试时得到 executed.In 下面的代码,控件转到第
jQuery.getJSON('/cart.js', function(cart){
之后是显示购物车计数正确的警告框。但是,警报消息下方的代码未被执行。我哪里错了。
function addtocart(prod_handle, variant_id) {
var found = false;
var x = 0;
jQuery.getJSON('/cart.js', function(cart) {
alert(cart.item_count);
x = cart.item_count;
if (x > 0) {
jQuery.getJSON('/product/' + prod_handle + '.js', function(product) {
var selected_prod_tags = products.tags;
for (var index = 0; index < selected_prod_tags.length; index++) {
if (selected_prod_tags[index] == 'jamshedpur' || selected_prod_tags[index] == 'ranchi') {
var city = selected_prod_tags[index];
}
}
jQuery.getJSON('/product/' + cart.items[0].handle + '.js', function(product) {
var cart_prod_tags = products.tags;
for (index = 0; index < cart_prod_tags.length; index++) {
if (cart_prod_tags[index] == city) {
found = true;
jQuery.post('/cart/add.js', {
quantity: 1,
id: variant_id
});
}
}
if (found == false) {
alert("Error!! Please select products from same city");
}
});
});
} else {
jQuery.post('/cart/add.js', {
quantity: 1,
id: variant_id
});
}
});
}
您 运行 在接下来的两个请求中未定义产品。这只是一个错字吗?
请记住,getJSON 要求它是有效的 JSON 否则它会无提示地失败,具体取决于您拥有的 jQuery 版本。
jQuery.getJSON('/product/' + prod_handle + '.js', function(product) {
var selected_prod_tags = products.tags;
您可能还想考虑将部分代码抽象出来以分离函数,例如add.js 电话。