简体   繁体   English

如何在 Ajax 调用(jQuery)之后更新 DOM?

[英]How can I update the DOM after Ajax Call (jQuery)?

How can I update the DOM with new record added element after call?调用后如何使用新记录添加元素更新 DOM?

Should I return the newly inserted db record contents on call back then append using jquery?我是否应该使用 jquery 在回叫时返回新插入的数据库记录内容然后 append?

Please advice if not enough data to answer question.如果没有足够的数据来回答问题,请提出建议。

$(function(){

        $('#postentry').submit(function() {
          var tyu = $('#tittle_ent').val();
          if(tyu.length <= 2)
            {alert('Enter More Text!'); return false;}else
            {
                $.post(  
                 "posts_in.php",  
                 $("#postentry").serialize(),  
                    function(data){  
                        //alert(data); 
                    }  
                );  
                return false;
            }
        });

    });

Each record look like this:每条记录如下所示:

<div id="post349" class="myclass">
This is some text.    
</div>

I'd use $.ajax() instead of $.post() :我会使用$.ajax()而不是$.post()

$.ajax({
    type:'post',
    url: 'posts_in.php',
    data: $("#postentry").serialize(),
    success:function(data){
        var id='post349';
        $('<div></div>').attr('id',id).addClass('myclass').html(data).appendTo('#records_wrapper');
    }
});

Quick demo of how the div will be appended and populated: http://jsfiddle.net/AlienWebguy/zuPA2/1/快速演示如何添加和填充 div: http://jsfiddle.net/AlienWebguy/zuPA2/1/

You can either return a complete record in the surrounding div from php or you can just get the newly inserted ID and build the record in javascript.您可以从 php 返回周围div中的完整记录,也可以获取新插入的 ID 并在 javascript 中构建记录。

In your JS file you could write this在你的 JS 文件中你可以写这个

 $.ajax({
    type: 'POST',
    url: posts_in.php,
    data: {
       action: 'updateDomAfterAjaxCall',
       post_id: postentry
   },

   success: function(data, textStatus, XMLHttpRequest) {

       var linkid = '#' + postentry;
       jQuery(linkid).html('');
       jQuery(linkid).append(data);

   },
   error: function(MLHttpRequest, textStatus, errorThrown) {
       alert(errorThrown);
   }
 });
 

In your backend code, you could do this.在您的后端代码中,您可以这样做。 In this case, I used PHP.在这种情况下,我使用了 PHP。

 function updateDomAfterAjaxCall() {

    $post_ID = $_POST['post_id'];
    // Carefully tranverse the dom to 
    // Update records of the DOM that is 
    // affected by the new entry
    // so that it behave almost like
    // headless browser
    // Target footer-postentry
    // header-postentry, sidebar-postentry, content-postentry

    $result = '';
    $record_from_db = ""

    $results.='<div class="myclass" >'.$record_from_db.'</div>';
    die($result);
  }
$.post(  
     "posts_in.php",  
     $("#postentry").serialize(),  
        function(data){  
            $('#myDivOnThePage').append(data);
        }  
 );  

If data contains new record, so yes, parse it and append where and like you want.如果数据包含新记录,那么是的,解析它并 append 在哪里和喜欢你想要的。

By creating a new jsfiddle.net with an example, I can help you a little bit more...通过使用示例创建一个新的 jsfiddle.net,我可以为您提供更多帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM