나는 장바구니에 항목을 추가하는 jQuery 함수를 빌드하는 데 도움이되도록 http://api.jquery.com/jquery.post/을 사용했습니다.성공/실패를 jQuery/Ajax 함수에 포함하기
내 결과이이며 작동합니다
jQuery(document).ready(function() {
// Attach a submit handler to the form
$('form[name="cart_quantity"]').submit(function(event) {
// Stop form from submitting normally and initiate function
event.preventDefault();
return addtocart(jQuery(this));
}); // eof Attach a submit handler to the form
// function
function addtocart(thisForm) {
// Get some values from elements on the page:
$("#contentss").fadeIn("slow");
action = thisForm.attr("action");
// Send the data using post
var posting = $.post(action, thisForm.serialize());
// Process post-results
posting.done(function(data) {
// find and put results in a div
var shoppingCartSidebox = $(data).find('div#shoppingcart').html();
var numbernotify = $(data).find('div.numbernotify').html();
$('div#shoppingcartdiv').html(shoppingCartSidebox);
$('div.numbernotify').html(numbernotify)
// initiate slideDown/slideUp
$(document.getElementById('contentss').style.display='none');
jQuery(document).ready(function() {
$("#shoppingcartdiv").slideDown();
setTimeout(function() {$("#shoppingcartdiv").slideUp()}, 7000);
}); // eof initiate slideDown/slideUp document ready
}); // eof Process post-results
} // eof function
}); // eof main document ready
나는 제품이 장바구니에 추가되지 않는 순간에 성공/실패 이벤트를 구현하고 싶습니다. 성공 이벤트는 이미 코딩되어 있으며 기본적으로 "// 결과 후 처리"이벤트 뒤에 정의됩니다.
그래서 성공과 비슷한 결과를 포함하는 실패 이벤트를 삽입해야합니다. 항목이 성공적으로 추가되었음을 사용자에게 경고하면 오류가 발생했다고 경고합니다. 그러나 나는 단지 물어보고 싶다. 예를 들어 사용자가 장바구니에 항목을 추가하는 동안 인터넷 연결이 끊어지는 경우와 같이 Fail 이벤트를 추가하는 경우 그것이 내가 본질적으로 찾고있는 것이기 때문에 ...
또한 나는 끔찍한 아마추어다고 생각하여 현재 코드와 개선 사항에 대한 조언을 매우 높이 평가할 것입니다.
like posting.done (function (data) {}); posting.fail (function (data) {// 실패 할 경우}); posting.always (함수 (데이터) {성공 또는 실패 여부와 관계없이 항상 수행}); – Aman
@Aman that worked! 환상적인 친구 감사합니다 :) – StuyvesantBlue
위대한 : 디 .. 행운 – Aman