我需要替换来自 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);
我正在尝试这种方法,但我不知道这是否是最好的方法。我感谢任何建议和帮助。
最佳答案
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,
));
关于php - 如何通过来自 php 的变量更改 jQuery 中的数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406423/