dongtuo5262 2013-08-04 21:26
浏览 40
已采纳

当我添加“next”函数时,jquery分页会中断

So I'm making an attempt at my first jquery code and I'm stuck.

Basically I'm making a simple pagination script, I've set the number of rows to display to 3 and when I view the results page it shows 3 out of the 21 rows returned in the search.

Now when I add the next function to the script to move from page to page and run a search the results no longer display the 3 rows but instead display all the rows. Can someone tell me what I am doing wrong?

Here is my code:

    <html>
<head>
<script src="jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

//Declaring Variables
var page = 1;
var per_page = 3;
var items = $('.row').length;
var page_last = Math.ceil(items/per_page);
}
//Set Page
function setPage(page) {
$('.row').slice(0, page * per_page).hide();
$('.row').slice(page * per_page - per_page, page * per_page).show();
$('.row').slice(page * per_page).hide();
$('#page_display').html('page ' + page);

}
//Next Button
$('.next').click(function() {
if (page < page_last) {
page++;
setPage(page);
}
});
//Previous Button
$('.prev').click(function() {
if (page > 1) {
page--;
setPage(page);
}
});

setPage(1);
});
</script>
</head>
<body>


<?php

$host = 'localhost';
$username = 'root';
$password = '';

try {
$dbh = new PDO("mysql:host=localhost;dbname=elitenv", $username,
$password);
} catch (PDOException $e) {
echo $e->getMessage();
}

$query = "SELECT * FROM `players` WHERE `id` > :val";
$stmt = $dbh->prepare($query);
$stmt ->bindValue(':val', $_POST['search']);
$row = $stmt->execute();

echo "<table border='1'>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo"<tr class='row'><td>";
echo $row['id'];
echo "</td></tr>";
}
echo "</table>";


?>
<div id="page_display"><br/>
<button type="button" class="prev">Previous</button>
<button type="button" class="next">Next</button>
</div>
</body>
</html>

If I remove the next button and prev button functions it will once again display 3 rows of 21.

  • 写回答

2条回答 默认 最新

  • douzhuangna6906 2013-08-04 21:45
    关注

    Remove the closing } below variable page_last

    see http://jsfiddle.net/hPM2H/

    also don't overwride the page_display div, you will lose your next and prev buttons :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示