[英]Display HTML table on page load
我正在嘗試在頁面加載時將URL響應顯示到表中。 但是我可以控制URL響應但不能將它們顯示在表中。這是我的js文件:
window.onload = function (){
var result = $.ajax({
type: "GET",
url: "http://localhost:8080/impExt",
dataType:"json", //to parse string into JSON object,
success: function(data){
if(data){
var len = data.length;
var txt = "";
if(len > 0){
for(var i=0; i<len; i++){
if(data[i].name && data[i].ext){
txt += "<tr><td>"+data[i].name+"</td><td>"+data[i].ext+"</td></tr>";
console.log(txt);
}
}
if(txt != ""){
$("#impExtTableID tbody").html(txt).removeClass("hidden");
}
}
}
},
error: function(jqXHR, textStatus, errorThrown){
alert('error: ' + textStatus + ': ' + errorThrown);
}
}).responseText;
return false;//suppress natural form submission
};
我已經嘗試了幾個與我的帖子相關的答案,但是沒有任何效果。 有人可以幫我在頁面加載時顯示此表嗎? 如果您需要我的索引頁,這里是:
<!DOCTYPE html>
<html lang="en" xmlns:margin-right="http://www.w3.org/1999/xhtml" xmlns:padding-bottom="http://www.w3.org/1999/xhtml"
xmlns:width="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table id = "impExtTableID" class= "hidden" >
<thead style="width:03%">
<th style="width:60%">Name</th>
<th style="width:22%">Ext</th>
</thead>
<tbody></tbody>
</table>
<script src = "js/jquery-3.2.1.min.js"></script>
<script src ="js/bootstrap.min.js"></script>
<script src = "js/impExt.js"></script>
</body>
</html>
hidden
類需要從table
刪除,而不是tbody
。 做了
$("#impExtTableID tbody").html(txt);
$("#impExtTableID").removeClass("hidden");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.