jQuery .find() on data from .ajax() call is returning “[object Object]” instead of div

前端 未结 17 1170
一生所求
一生所求 2020-12-07 22:05

Trying to find div element with id=\"result\" in returned data from .ajax() using .find(). Unfortunately, alert(res

相关标签:
17条回答
  • 2020-12-07 22:43

    You might have to do something like

    var content= (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d
    

    then you should be able to use

    result = $(content).find("#result")
    
    0 讨论(0)
  • 2020-12-07 22:46

    This worked for me, you just need to put .html() on the end of - $(response).find("#result")

    0 讨论(0)
  • 2020-12-07 22:48

    The jQuery find() is returning a jQuery object that wraps the DOM object. You should be able to work with that object to do what you'd like with the div.

    0 讨论(0)
  • 2020-12-07 22:50

    Specify dataType: "html".

    If you do not jQuery will guess the requested data type (check: http://api.jquery.com/jQuery.ajax/). My guess is that in your case response was a String rather than a DOMObject. Obviously DOM methods won't work on a String.

    You could test that with console.log("type of response: " + typeof response) (or alert("type of response:" + typeof response), in case you don't run Firebug)

    0 讨论(0)
  • 2020-12-07 22:50

    try if( $(response).filter('#result').length ) // do something

    0 讨论(0)
  • 2020-12-07 22:54
    $.ajax({
        url: url,
        cache: false,
        success: function(response) {
            $('.element').html(response);
        }
    });
    
    < span class = "element" >
        //response
        < div id = "result" >
            Not found 
        </div> 
    </span>
    
    var result = $("#result:contains('Not found')").text();
    console.log(result); // output: Not found
    
    0 讨论(0)
提交回复
热议问题