我正在尝试使用来自ajax html响应的内容更新div。我相信我的语法正确,但是div内容已替换为整个HTML页面响应,而不仅仅是html响应中选择的div。我究竟做错了什么?
<script> $('#submitform').click(function() { $.ajax({ url: "getinfo.asp", data: { txtsearch: $('#appendedInputButton').val() }, type: "GET", dataType : "html", success: function( data ) { $('#showresults').replaceWith($('#showresults').html(data)); }, error: function( xhr, status ) { alert( "Sorry, there was a problem!" ); }, complete: function( xhr, status ) { //$('#showresults').slideDown('slow') } }); }); </script>
您正在设置HTML格式的#showresults内容data,然后将其替换为本身,这没有多大意义? 我猜你真正#showresults在哪里寻找返回的数据,然后#showresults用ajax调用中的html用html 更新DOM中的元素:
#showresults
data
$('#submitform').click(function () { $.ajax({ url: "getinfo.asp", data: { txtsearch: $('#appendedInputButton').val() }, type: "GET", dataType: "html", success: function (data) { var result = $('<div />').append(data).find('#showresults').html(); $('#showresults').html(result); }, error: function (xhr, status) { alert("Sorry, there was a problem!"); }, complete: function (xhr, status) { //$('#showresults').slideDown('slow') } }); });