我正在尝试使用 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>
最佳答案
您正在设置 data
的 #showresults
的 html,然后将其替换为自身,这没有多大意义?
我猜你真的想在返回的数据中找到 #showresults
,然后使用 ajax 中的 html 更新 DOM 中的 #showresults
元素调用:
$('#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')
}
});
});
关于jquery - 使用 jQuery ajax 响应 html 更新 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18064666/