这个脚本应该每秒运行一个jQuery Ajax脚本,它应该获取所有较新的帖子,然后将它们放在原始帖子之上。但目前没有任何事情发生,它加载了最初的帖子,但没有别的。 Firebug控制台报告没有错误,或者根本没有发送Ajax请求。你能看到这个剧本有什么不妥吗?谢谢:))
源代码 - index.php(我遗漏了css):
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Twitter Style load more results.</title>
<link href="frame.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(function () {
setInterval(oneSecondFunction, 1000);
});
function oneSecondFunction() {
var ID = $(this).attr("id");
if (ID) {
$("#more" + ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg=" + ID,
cache: false,
success: function (html) {
$("ol#updates").prepend(html);
$("#more" + ID).remove();
}
});
} else {
}
return false;
};
});
</script>
</head>
<body>
<div id='container'>
<ol class="timeline" id="updates">
<?php
$sql=mysql_query("select * from updates ORDER BY item_id DESC LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg_id=$row['item_id'];
$message=$row['item_content'];
?>
<li>
<?php echo $message; ?>
</li>
<?php } ?>
</ol>
</div>
</body>
</html>
ajax_more.php -
<?php
include("config.php");
if(isset($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$result=mysql_query("select * from updates where item_id<'$lastmsg' order by item_id desc limit 9");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$msg_id=$row['item_id'];
$message=$row['item_content'];
?>
<li>
<?php echo $message; ?>
</li>
<?php
}
?>
<?php
}
?>
答案 0 :(得分:0)
我引用了它获取id的部分,因为'this'并不是指任何东西。
<script type="text/javascript">
$(function() {
setInterval(oneSecondFunction, 1000);
});
function oneSecondFunction() {
//var ID = $(this).attr("id");
var ID = '1';
if(ID){
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").prepend(html);
$("#more"+ID).remove();
}
});
}
return false;
}
</script>
答案 1 :(得分:0)
一开始的一些代码看起来不对。这应该工作。它将每秒调用oneSecondFunction:
$(document).ready(function() {
setInterval(oneSecondFunction, 1000);
function oneSecondFunction() {
//console.log('run oneSecondFunction');
var ID = $(this).attr("id");
if(ID) {
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({ type: "POST", url: "ajax_more.php", data: "lastmsg="+ ID, cache: false,
success: function(html){
$("ol#updates").prepend(html);
$("#more"+ID).remove();
}
});
} else {
} return false; };
});