使用jQuery ajax响应html更新div。

时间:2022-10-09 14:53:47

I am trying to update a div with the content from an ajax html response. I beleive I have the syntax correct, however the div content gets replaced with the entire HTML page response, instead of just the div selected in the html response. What am I doing wrong?

我正在尝试用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>

3 个解决方案

#1


54  

You are setting the html of #showresults of whatever data is, and then replacing it with itself, which doesn't make much sense ?
I'm guessing you where really trying to find #showresults in the returned data, and then update the #showresults element in the DOM with the html from the one from the ajax call :

您正在设置#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')
        }
    });
});

#2


1  

It's also possible to use jQuery's .load()

还可以使用jQuery .load()

$('#submitform').click(function() {
  $('#showresults').load('getinfo.asp #showresults', {
    txtsearch: $('#appendedInputButton').val()
  }, function() {
    // alert('Load was performed.')
    // $('#showresults').slideDown('slow')
  });
});

unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

We could modify the example above to use only part of the document that is fetched:

与$.get()不同,允许我们指定要插入的远程文档的一部分。这是通过url参数的特殊语法实现的。如果字符串中包含一个或多个空格字符,则假定第一个空格后的字符串部分是jQuery选择器,该选择器确定要加载的内容。我们可以修改上面的示例,只使用获取的文档的一部分:

$( "#result" ).load( "ajax/test.html #container" );

When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

当这个方法执行时,它将检索ajax/test的内容。然后jQuery解析返回的文档,找到具有容器ID的元素。该元素及其内容被插入到具有结果ID的元素中,而检索到的其余文档则被丢弃。

#3


0  

Almost 5 years later, I think my answer can reduce a little bit the hard work of many people.

差不多5年过去了,我认为我的答案可以减少很多人的努力。

Update an element in the DOM with the HTML from the one from the ajax call can be achieved that way

使用来自ajax调用的HTML更新DOM中的元素,可以通过这种方式实现

$('#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')
     },
});

or with replaceWith()

或与replaceWith()

// codes

success: function (data){
   $('#showresults').replaceWith($('#showresults',data));
},

#1


54  

You are setting the html of #showresults of whatever data is, and then replacing it with itself, which doesn't make much sense ?
I'm guessing you where really trying to find #showresults in the returned data, and then update the #showresults element in the DOM with the html from the one from the ajax call :

您正在设置#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')
        }
    });
});

#2


1  

It's also possible to use jQuery's .load()

还可以使用jQuery .load()

$('#submitform').click(function() {
  $('#showresults').load('getinfo.asp #showresults', {
    txtsearch: $('#appendedInputButton').val()
  }, function() {
    // alert('Load was performed.')
    // $('#showresults').slideDown('slow')
  });
});

unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

We could modify the example above to use only part of the document that is fetched:

与$.get()不同,允许我们指定要插入的远程文档的一部分。这是通过url参数的特殊语法实现的。如果字符串中包含一个或多个空格字符,则假定第一个空格后的字符串部分是jQuery选择器,该选择器确定要加载的内容。我们可以修改上面的示例,只使用获取的文档的一部分:

$( "#result" ).load( "ajax/test.html #container" );

When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

当这个方法执行时,它将检索ajax/test的内容。然后jQuery解析返回的文档,找到具有容器ID的元素。该元素及其内容被插入到具有结果ID的元素中,而检索到的其余文档则被丢弃。

#3


0  

Almost 5 years later, I think my answer can reduce a little bit the hard work of many people.

差不多5年过去了,我认为我的答案可以减少很多人的努力。

Update an element in the DOM with the HTML from the one from the ajax call can be achieved that way

使用来自ajax调用的HTML更新DOM中的元素,可以通过这种方式实现

$('#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')
     },
});

or with replaceWith()

或与replaceWith()

// codes

success: function (data){
   $('#showresults').replaceWith($('#showresults',data));
},