使用 jQuery 每 10 秒用 php 数据刷新一个 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15998853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh a div with php data every 10 seconds using jQuery
提问by New_programmer
Am trying to refresh data stored in a div every 10 seconds using jQuery.
我正在尝试使用 jQuery 每 10 秒刷新一次存储在 div 中的数据。
My HTML code is:
我的 HTML 代码是:
<!DOCTYPE html>
<head>
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
setInterval(function() {
$("#latestData").load("getLatestData.php #latestData");
}, 10000);
});
</script>
</head>
<body>
<div id = "latestData">
</div>
</body>
</html>
And the PHP code I am using (temporarily as I know this won't change due to the same "data"):
我正在使用的 PHP 代码(暂时我知道这不会因为相同的“数据”而改变):
<?php
echo "test";
?>
However, it isn't even showing "test" on the html page.. could anyone suggest where I have gone wrong?
但是,它甚至没有在 html 页面上显示“测试”.. 谁能建议我哪里出错了?
Many thanks
非常感谢
采纳答案by ulentini
jQuery load
method works in a different way. Try reading its documentation.
jQueryload
方法以不同的方式工作。尝试阅读它的文档。
You don't have to specify destination element ID twice, remove the second one, like this:
您不必两次指定目标元素 ID,删除第二个,如下所示:
$("#latestData").load("getLatestData.php");
回答by Jason Lipo
Here's a way that will solve what you want to achieve, using the $.get
method in jQuery:
这是一种使用$.get
jQuery中的方法可以解决您想要实现的目标的方法:
$(document).ready(function () {
setInterval(function() {
$.get("getLatestData.php", function (result) {
$('#latestData').html(result);
});
}, 10000);
});
回答by vishal
If you want to refresh message count just use this code:
如果您想刷新消息计数,只需使用以下代码:
$(document).ready(function () {
setInterval(function () {
$("#ID").load();
}, 1000);
});