首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery AJAX刷新多个Div(重复刷新)

JQuery AJAX刷新多个Div(重复刷新)
EN

Stack Overflow用户
提问于 2016-02-24 17:35:22
回答 2查看 3.1K关注 0票数 0

我花了几个小时在这里寻找答案,但我找不到答案。我的自动刷新工作,问题是它刷新了彼此内部的所有div。例如,我有一个计算机辅助调度应用程序,它为可用的单元、分配的呼叫和挂起的呼叫设置了一个分区,但是当它刷新时,它会将所有这三个div放在一起!

代码语言:javascript
复制
var auto_refresh = setInterval(
function () {
    $('#avunits').load('supervisor.php #avrefresh');
}, 5000);

另一个问题是,我尝试使用多个div语法来实现它们,但这只适用于第一个div,而忽略了另外两个:

代码语言:javascript
复制
var auto_refresh = setInterval(
function () {
    $('#avunits,#assigned,#pending').load('supervisor.php .refresh');
}, 5000);

在第二个示例中,它将所有三个div放入可用的Units中,但不更新分配的或挂起的。

最后,我试着把这三种方法放在一起,但仍然没有用:

代码语言:javascript
复制
var auto_refresh = setInterval(
function () {
    $('#avunits').load('supervisor.php #avrefresh', function (){
        $('#assigned').load('supervisor.php #avrefresh', function (){
            $('#pending').load('supervisor.php #penrefresh');
})})}, 5000);

我想要做的是刷新所有三个div并保持它们分开。

有关守则:

代码语言:javascript
复制
    // All of the code is within the same webpage. I plan to separate them to their own pages once everything works.
// This is the working version. Currently, the AVRefresh works, but if I do what I said in my Stack Overflow post, it doesn't work.

<head>
    <?if($_GET['info'])
    {   }
    else
    {?>
        <script src="java/jquery.js"></script>
        <script type="text/javascript">
        var auto_refresh = setInterval(
        function () {
             // If I use "#avunits" after supervisor.php it embedds a second DIV. So, I made a new one so as to not mess up my CSS
            $('#avunits').load('supervisor.php #avrefresh');
            }, 5000);
        </script>
    <?}?>       
    <title>WebCAD Dispatch</title>
    <link rel="stylesheet" type="text/css" href="supervisor_base.css">
    <link rel="stylesheet" type="text/css" href="callview_assigned.css">
    <link rel="stylesheet" type="text/css" href="callview_pending.css">
</head>
<body>



// (Continued)


<div id="background">
    <div id="fixedwidth">
        <div id="sidebar">
            <div id="avunits">
                <div id="avrefresh">
                        // Various MySQL Queries
                        // If the user is a supervisor, staff, or admin, and is not banned and approved.
                        {
                            if(isset($_GET['info']) || isset($_GET['refresh']))
                            {
                                // Various MySQL Queries                                    
                                // HTML/PHP Readouts/Echoes

                            }
                            else
                            {
                                // Various MySQL / PHP / HTML Codes that retrieve the users
                                // And prints out all units, and if busy, their status and Call#
                            }
                        }
                        else
                        {
                            // Javascript back to index.php for login prompt
                        }
                </div>
            </div>
            <div id="callview">
                <div id="assigned">
                    <div id="assigned_refresh">
                        // Various MYSQL/PHP Calls to retrieve incident information and assigned units (if any)
                    </div>
                </div>
                <div id="pending">
                    <div id="pending_refresh">
                        // Various MYSQL/PHP Calls to retrieve incident information
                    </div>
                </div>
            </div>
        </div>
    </div>
 </div>

// (Continued)

</body>

为了保护应用程序的完整性,我删除了不相关的MySQL/PHP代码。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-24 18:43:27

听起来,您正在尝试加载一个页面,然后将页面的不同片段替换为加载页面的不同片段。除非您想要三次加载相同的页面,否则您将无法使用load()完成这一任务。相反,您需要使用$.get()并自己更新片段:

代码语言:javascript
复制
$.get("refresh.php")
    .done(function(r) {
        var newDom = $(r);
        $('#avunits').replaceWith($('#avunits',newDom));
        $('#assigned').replaceWith($('#assigned',newDom));
        $('#pending').replaceWith($('#pending',newDom));
     });

如果您绝对必须使用load,则需要单独和显式地处理每个片段。正如Xorifelse的答案中提到的,与许多jQuery方法不同,get()运行第一个匹配的元素,而忽略其余的元素。因此,如果使用$(".refresh").get(),将只更新类"refresh“的第一个元素。

代码语言:javascript
复制
$('#avunits').load('supervisor.php #avunits_refresh');
$('#assigned').load('supervisor.php #assigned_refresh');
$('#pending').load('supervisor.php #pending_refresh');
票数 1
EN

Stack Overflow用户

发布于 2016-02-24 18:19:18

我不知道您想通过使用'supervisor.php .refresh'$('#.refresh')supervisor.php #avrefresh中的if然后$('#avunits,#assigned,#pending').load('supervisor.php .refresh'); }, 5000);子句或最奇怪的$('#avunits,#assigned,#pending').load('supervisor.php .refresh'); }, 5000);来完成什么任务

就像你说的那样。将这些div #avunits,#assigned,#pending中的HTML代码更改为load('supervisor.php .refresh')的输出

但是,我甚至不确定jQuery是否将此作为有效的输入排除在外,并且可能会选择找到的第一个元素并跳过其余的元素,对于load函数,我不会冒险猜测您从哪里获得该文档。与下面的注释一样,它最多需要一个html ID,而不是一个类,并忽略文档中声明的其余部分(甚至包括您的选择器)。

load()需要一个有效的URL。如果您想使用supervisor.php做不同的事情,您应该通过执行load('supervisor.php?action=assigned')来使用一个有效的load('supervisor.php?action=assigned'),并在PHP脚本中用if / if($_GET['action'] == 'assigned'){} elseif($_GET['action'] == 'pending'){} else {//code for malicous data}子句分隔它们,执行if($_GET['action'] == 'assigned'){} elseif($_GET['action'] == 'pending'){} else {//code for malicous data}等操作来返回不同的输出。

如果用户向ajax请求发送恶意数据,则该示例中的user语句应该是一个操作。

警告

如果不更改load() url,jQuery甚至浏览器都有可能缓存响应。您需要清除缓存或使用带有$_GET请求的链接来分隔缓存。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35609138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档