[英]Update old ajax code to current JQUERY version
我有一個舊的AJAX代碼,可在JQUERY 1.8.3中使用,但在更高版本中無法使用。 當我嘗試使代碼保持最新並嘗試避免不推薦使用的代碼時,我需要更新腳本。
當前代碼
$('.productlist input[type=submit]').bind('click', function () {
var i = 0;
var a = $(this).closest('.productlist2').find('input[data-productid][value!=]').length;
$(this).closest('.productlist,.WPproductlist').find('input[data-productid][value!=]').each(function () {
if ($(this).attr('data-produktfarve') != "") {
$.ajax({
url: '?ProductID=' + $(this).attr('data-productid') + '&EcomOrderLineFieldInput_farvevalg=' + $(this).attr('data-produktfarve') + '&Quantity=' + parseFloat($(this).val()) + '&cartcmd=add',
success: function (data) {
i++;
if (i == a) {
window.location.reload();
}
}
});
} else {
$.ajax({
url: '?ProductID=' + $(this).attr('data-productid') + '&Quantity=' + parseFloat($(this).val()) + '&cartcmd=add',
success: function (data) {
i++;
if (i == a) {
window.location.reload();
}
}
});
}
});
setTimeout(function () {
window.location.reload();
}, 500);
return;
});
我嘗試了各種新版本的JQUERY get和post請求,但無濟於事。
嘗試這個,
$(function(){
$('.productlist input[type=submit]').on('click', function () {
var i = 0;
var a = $(this).closest('.productlist2')
.find('input[data-productid][value!=""]').length;
$(this).closest('.productlist,.WPproductlist')
.find('input[data-productid][value!=""]').each(function () {
var myurl = '?ProductID=' + $(this).data('productid');
if ($(this).data('produktfarve') != "") {
myurl+='&EcomOrderLineFieldInput_farvevalg='+$(this).data('produktfarve');
}
var qty=this.value;
$.ajax({
url: myurl+ '&Quantity=' + parseFloat(qty) + '&cartcmd=add',
success: function (data) {
i++;
if (i == a) {
window.location.reload();
}
}
});
});
setTimeout(function () {
window.location.reload();
}, 500);
return;
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.