我想刷新一張表格,並填充JSON數據,例如每5秒鐘一次。用JQUERY刷新AJAX表格
該表應該刷新而不需要完整的網站刷新。應添加新項目,應刪除丟失的項目。我如何使用AJAX刷新它?
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<table face="comic sans ms" border="1" id="usertable"></table>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON('./url.php', function(data) {
var cars = data.rec.vehicles.vehicles;
$.each(cars, function(key, data){
console.log(data.carName);
var vint=data.vin;
var coopers="";
var tanken="";
var drive="MAN";
var fuelfarbe="#01DF3A";
var prozent="%";
var v=vint.substring(3,7);
if(v=="SX31"){coopers=" S";}
if(v=="SW71"){coopers=" SD";}
if(data.auto=="Y"){drive="AUT";}
if(data.fuelState<=25){tanken="20 Freiminuten für Vollbetankung"; fuelfarbe="#FF4000";}
$('#usertable').append(
function() {
return "<tr>"+
"<td>"+data.carName+"</td>"+
"<td>"+data.model+coopers+"</td>"+
"<td>"+drive+"</td>"+
"<td>"+data.fuelState+prozent+"</td>"+
//"<td>"+data.address+"</td>"+
"<td>"+data.licensePlate+"</td>"+
"<td>"+tanken+"</td>"
"<tr>";
}
);
})
});
});
</script>
</body>
</html>
嘗試使用[window.setInterval](https://developer.mozilla.org/ en-US/docs/Web/API/Window.setInterval) –
我強烈建議你看看使用類似Kno的東西ckout這樣的東西。否則,當需要做出改變時,你會把頭髮拉出來。 http://knockoutjs.com – ShaneBlake