'Destination DIV' disappearing in simple jQuery AJAX call

前端 未结 5 417
说谎
说谎 2021-01-24 21:35

Edit: Important: It is extremely unlikely that anybody who has come across this will have the exact same problem as me - check my answer below for my very obscu

相关标签:
5条回答
  • 2021-01-24 22:19

    Wow! Well that was crazy.

    Turns out the problem is because of this code that I have in an included JS file:

     jQuery.fn._init = jQuery.fn.init
     jQuery.fn.init = function(selector, context) {
         if (typeof selector === 'string') {
             return jQuery.fn._init(selector, context).data('selector', selector);
         }
         return jQuery.fn._init(selector, context);
     };
    

    I am using jQuery 1.2.6.

    The code above came from this post How do I get a jQuery selector's expression as text?. i think the author since rewrote it, but its breaking this for some strange reason.

    The DIV just disappears - but i got thrown because replaceWith() DID work and so did append(). I guess some of the other methods mus internally being tripped up by this but those two werent.

    Thanks for everyones quick suggestions. I'm sure they'll help out others some point.

    0 讨论(0)
  • 2021-01-24 22:20

    Check the ID of your div, make sure the correct ID is being passed on to jquery.

    0 讨论(0)
  • 2021-01-24 22:25

    Perhaps try this?

    $.get("/woops/store/cartsummary", {}, function(data) {
        $("#cart").html(data); 
    });
    
    0 讨论(0)
  • 2021-01-24 22:32
    append
    

    does not replace your div. It pastes the data at the back of your div.

    You could try

    replaceWith( content ) 
    

    instead.

    replaceWith in jQuery documentation

    0 讨论(0)
  • 2021-01-24 22:33

    try

    $.get("/woops/store/cartsummary", {}, function(data) {
        $("#cart").html(data); 
    });
    

    (jQuery docs)

    0 讨论(0)
提交回复
热议问题