the*_*eld 37 ajax jquery get asp-classic replacewith
我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)
ade*_*neo 70
你正在设置#showresults什么data是html ,然后用它自己替换它,这没有多大意义?
我猜测你在#showresults返回的数据中找到了什么,然后#showresults使用来自ajax调用的html 更新DOM中的元素:
$('#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')
}
});
});
Run Code Online (Sandbox Code Playgroud)
差不多5年后,我想我的回答可以减少很多人的辛勤工作。
更新一个 可以通过这种方式使用来自 ajax 调用的 HTMLDOM 中元素
$('#submitform').click(function() {
$.ajax({
url: "getinfo.asp",
data: {
txtsearch: $('#appendedInputButton').val()
},
type: "GET",
dataType : "html",
success: function (data){
$('#showresults').html($('#showresults',data).html());
// similar to $(data).find('#showresults')
},
});
Run Code Online (Sandbox Code Playgroud)
或与 replaceWith()
// codes
success: function (data){
$('#showresults').replaceWith($('#showresults',data));
},
Run Code Online (Sandbox Code Playgroud)