我试着做进度条,css代码是这样的:
#result{
background:#8c8f91;
margin-left: 15px;
margin-right: auto;
table-layout: fixed;
border-collapse: collapse;
z-index: -1; position:relative;
width: 0%;
}
我想用 Ajax 更新我的进度条:
$(function worker(){
// don't cache ajax or content won't be fresh
$.ajaxSetup ({
cache: false,
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker, 4500);
}});
$("#result").css("width"," <?php echo json_encode($percent); ?>");
$("#result").load("http://localhost/test6/select-oki.php #result").fadeIn();
}
// end
});
我想用“$percent”更新“width”值,但它没有从中获取值。我认为问题在于:
$("#result").css("width"," <?php echo json_encode($percent); ?>");
最佳答案
关于原始代码的一些事情。 $percent 未在 js 中定义,至少您显示的代码是这样。您甚至在尝试加载之前就请求 json_encode。我没有测试过这个,只是我注意到的一些东西。
考虑这种方法
$(function worker(){
$.ajax({
cache: false,
url:'http://localhost/test6/select-oki.php',
success: function(data) {
var response = JSON.parse(data);
$("#result").css("width",response['percent']).fadeIn();
},
error: function(xhr, ajaxOptions, thrownError) {
console.log('error!');
}
});
});
从 PHP 回显你的数据 echo json_encode(['percent'=>$percent]);
关于php - 在 Ajax 中调用 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47482404/