简体   繁体   English

使用jQuery刷新包含PHP代码的div问题

[英]problem refreshing div containing php code using jquery

I have read 3 posts on SO about how to do this, but its not working for some reason. 我已经阅读了3则关于如何执行此操作的帖子,但是由于某种原因,它不起作用。

On the page index.php, i have this script: 在index.php页面上,我有以下脚本:

<script type="text/javascript">

function update() {
  $.get("index.php", function(data) {
    $("#uploadcount").html(data);
    window.setTimeout(update, 5000);
  });
}

</script>

and then this div, also in index.php 然后这个div,也在index.php中

<div id=uploadcount>
<?
$result = mysql_query("SELECT * FROM mydb");
$num_rows = mysql_num_rows($result);
echo "<h1>$num_rows</h1><h2>rows</h2>";
?>
</div>

The php is displaying the row count fine, but it wont refresh the number. php显示行数很好,但不会刷新该数字。
(i have latest jquery build included in head of index.php) (我在index.php的头中包含了最新的jquery版本)

Try enclosing your div ID in double quotes: 尝试将div ID括在双引号中:

<div id="uploadcount">

Also, put your jQuery within a document.ready block, and the call to setTimeout should not be inside the $.get method: 另外,将您的jQuery放在document.ready块中,对setTimeout的调用不应在$ .get方法内:

<script type="text/javascript">
$(document).ready(function() {
    function update() {
      $.get("index.php", function(data) {
        $("#uploadcount").html(data);
      });
    }
    window.setTimeout(update, 5000);
});
</script>

If you want to selectively retrieve something from a document, you can use $.load with a selector, eg: 如果要有选择地从文档中检索内容,可以将$ .load与选择器一起使用,例如:

$("#uploadcount").load("index.php #uploadcount");

http://docs.jquery.com/Ajax/load http://docs.jquery.com/Ajax/load

That only works with jQuery 1.2 and above. 仅适用于jQuery 1.2及更高版本。

Try using this for your update function 尝试将其用于更新功能

function update() {
    $("#uploadcount").load("index.php #uploadcount")
    window.setTimeout(update, 5000);
}

found a solution that works great 找到了一个很好的解决方案

<script type="text/javascript">
$(document).ready(function() {
setInterval(function()
{
     $('#uploadcount').fadeOut("fast").load('uploadcount.php').fadeIn("slow");
}, 10000);

});
</script>


<div id="uploadcount">
<? include("uploadcount.php");?>
</div>

Thanks all to helped. 谢谢大家的帮助。

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

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