0

我需要为来自 php.php 的变量替换值 17:

$(function() {
 $("#test").paginate({
  count    : 17,
  start    : 1,
  display  : 12,
  border   : true
  ...
 });
});

我试过这个(没用):

$(function(){
$("#test").paginate({
 count   : $.post("php.php",function(result){ console.log(result['count']) }),
 start   : 1;
 display : 12;
 border  : true
 ...
});

php.php

$query = mysql_query("SELECT * FROM test");
$count = mysql_num_rows($query);
json_encode($count);

我正在尝试这种方式,但我不知道这是否是最好的方式。我感谢任何建议和帮助。

4

1 回答 1

1

jQuery Ajax 函数是异步的,或者换句话说,立即返回,然后在完成时调用回调。您需要在回调中设置计数,如下所示:

$(function(){
    $.post("php.php",function(result){ 
        $("#test").pag({
            count : result
        });
    });
});

根据我们的评论,对于多个值,您需要类似

$(function(){
    $.post("php.php",function(result){ 
        $("#test").pag({
            count : result.count,
            start : result.start,
            display : result.display
        });
    });
});

PHP:

$query = mysql_query("SELECT * FROM test");
$count = mysql_num_rows($query);
echo json_encode(array(
    'count' => $result,
    'start' => 7,
    'display' => 10,
));
于 2012-05-02T01:03:15.177 回答