我想用div替換ajax中的消息的默認窗口,例如我想用div替換此代碼:alert(data.status + ": " + data.message);
。Ajax函數替換消息
這是我的Ajax代碼:
//Start ajax code
if(!url){
url = jQuery('#product_addtocart_form').attr('action');
}
url = url.replace("checkout/cart","ajax/index");
var data = jQuery('#product_addtocart_form').serialize();
data += '&isAjax=1';
jQuery('#ajax_loader').show();
try {
jQuery.ajax({
url: url,
dataType: 'json',
type : 'post',
data: data,
success: function(data){
jQuery('.messages').remove();
jQuery('#ajax_loader').hide();
//alert(data.status + ": " + data.message);
if(jQuery('.header .links')){
jQuery('.header .links').replaceWith(data.toplink);
}
if(jQuery('.mini-cart')){
jQuery('.mini-cart').replaceWith(data.sidebar);
var status = data.status;
showMessage(status.toLowerCase(), data.message);
}
// Create below function in your jquery
function showMessage(txt, type) {
var html = '<div id="messages_product_view"><ul class="messages"><ul class="messages"><li class="'+type+'-msg"><ul><li>' + txt + '</li></ul></li></ul></div>';
jQuery('.messages').update(html);
}
}
});
} catch (e) {
}
//End ajax code
在這段代碼,現在我有這樣的錯誤:
TypeError: jQuery(...).update is not a function
jQuery('.messages').update(html);
什麼是更新函數,嘗試使用.html()。 jQuery('。messages')。html(html); –
什麼是更新應該做的?這不是一個標準的jquery方法,那麼爲什麼你會期望它存在? – Liam
嗨,可以使用HTML來代替更新,但
是空白我沒有任何數據 – Robert