2016-09-27 58 views
-1

我想用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); 
+2

什么是更新函数,尝试使用.html()。 jQuery('。messages')。html(html); –

+0

什么是更新应该做的?这不是一个标准的jquery方法,那么为什么你会期望它存在? – Liam

+0

嗨,可以使用HTML来代替更新,但

是空白我没有任何数据 – Robert

回答

1

为text/html设为您可以使用这样的元素...

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').html(html); 

使用的.html()函数,而不是.update()

+0

是好的,但

是空白的,我没有任何数据 – Robert

+0

尝试控制台并查看showMessage函数是否正在调用,并在追加之前正确发送数据。 –

+0

我是begginer,我怎么能做到这一点? – Robert

2

试试

$('#messages_product_view').append(html); 
+0

相同,这个div是空白的

Robert

+0

$('#messages_product_view')。append(html); –