简体   繁体   English

如何在不重新加载整页的情况下刷新特殊div?

[英]How refresh particular div without reload whole page?

<div id="success_hold">
<?php
    if($row['mandate_end']!=date('Y-m-d') && $row['job_position']=='unhold')
    {
        echo '<span class="badge badge-success">Open</span>';
    }
    else
    {
        echo '<span class="badge badge-warning">Hold</span>';
    }
?>
</div>

<script>
    $(document).ready(function(){
        var flag =  1;
        $(".hold").click(function(){
            job_id = this.id;
            if(flag == 1)
            {
                $.ajax({
                    type:"POST",
                    data:{"job_id":job_id},
                    url:"<?php echo base_url(); ?>pausehold",
                    success:function(data){
                        $("#success_hold").load("#success_hold");
                    }
                });
                flag = 0;
            }
            else
            {
                $.ajax({
                    type:"POST",
                    data:{"job_id":job_id},
                    url:"<?php echo base_url(); ?>resumehold",
                    success:function(data){
                        $("#success_hold").load("#success_hold");
                    }
                });
                flag = 1;
            } 
        }); 
    });
</script>

I have div where id="success_hold" . 我有div,其中id="success_hold" Now, what happens when I click on class="hold" I am updating my data. 现在,当我点击class="hold"时会发生什么我正在更新我的数据。 Now, I want to refresh my <div id="success_hold"> without reload whole page where I am using .load() function But the problem is that when I use .load() the function shows the whole page in success_hold . 现在,我想刷新我的<div id="success_hold">而不重新加载我正在使用.load()函数的整个页面但问题是当我使用.load()该函数在success_hold显示整个页面。 So, How can I fix this issue? 那么,我该如何解决这个问题呢? I want to refresh only success_hold . 我想只刷新success_hold

Thank You 谢谢

The problem is because load() is used to make another AJAX request and place the a portion of the HTML retrieved from that request in the target element. 问题是因为load()用于发出另一个AJAX请求,并将从该请求中检索到的HTML的一部分放在目标元素中。

As you're already making an AJAX request to get the data, which is presumably HTML, you simply need to append() data to the container. 由于您已经在制作AJAX请求以获取数据(可能是HTML),因此您只需append()数据append()到容器即可。

Also note that the only difference between the two sides of your condition is the URL the request is sent to, so you can easily DRY this code up: 另请注意,您的条件双方之间的唯一区别是请求发送到的URL,因此您可以轻松地干掉此代码:

$(document).ready(function() {
  var flag = true;

  $(".hold").click(function() {
    var url = '<?php echo base_url(); ?>' + (flag ? 'pausehold' : 'resumehold');
    flag = !flag;

    $.ajax({
      type: "POST",
      data: { job_id: this.id },
      url: url,
      success: function(data) {
        $("#success_hold").append(data);
      }
    });
  });
});

If data contains the entire page, then you should change that PHP file to return only the relevant HTML as it will help to speed up the request. 如果data包含整个页面,那么您应该更改该PHP文件以仅返回相关的HTML,因为它将有助于加快请求。 If, for whatever reason, you can't do that then you can extract the required element from the response like this: 如果由于某种原因,你不能这样做,那么你可以从响应中提取所需的元素,如下所示:

$("#success_hold").append($(data).find('#success_hold'));
success:function(data){
    $("#success_hold").load("#success_hold");
}

This is your success method in ajax. 这是你在ajax中的成功方法。 You want to pur the content of data into your div #success_hold ? 您想将数据内容清除到您的div #success_hold中吗?

If you want that, just do this : 如果你想要,只需这样做:

success:function(data){
    $("#success_hold").html(data);
}

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

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