在 HTML table 中附加行 jquery 不起作用
Append rows in HTML table with jquery is not working
我正在使用 for
循环在 HTML table 中使用 jquery
追加多行。
for
循环 (下面的代码) 在 ajax
调用的响应上运行,其中 returns 一个 array of arrays
.但是只有 1 行被添加到 table 而不管否。数组中的元素。
实际响应对象比较大,所以附上一个Google Drive link for a reference
如何附加 table 中的所有行?
代码片段(使用Jquery)
$(document).ready(function() {
$('#ghsubmitbtn').on('click', function(e) {
var data = JSON.stringify(JSONObj);
$.ajax({
url: 'https://api-crt.cert.havail.sabre.com/v4.2.0/shop/flights?mode=live&limit=50&offset=1',
method: 'POST',
contentType: "application/json; charset=utf-8",
data: data,
headers: {
"Authorization": 'Bearer ' + bat
},
success: function(data) {
var BookBtn = '<input type="submit" value="Book" name="book" class="btn btn-primary">';
for (var v = 0; v <= 999; v++) {
for (var b = 0; b <= 999; b++) {
for (var m = 0; m <= 999; m++) {
var row = $('<tr><td>' + data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].SequenceNumber + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].OperatingAirline.Code + '</td><td>' +
BookBtn + '</td></tr>');
$('#tblData').append(row);
}
}
}
}
});
});
问题
JSON Exception in the console for an undefined JSON property
分辨率
Safely Access Deeply Nested Values In JavaScript
Read this 文章,以避免将来出现 JSON 异常。
更正
if (data && data.OTA_AirLowFareSearchRS && data.OTA_AirLowFareSearchRS.PricedItineraries && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v] && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b] && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m]) {
// Build row using nested JSON
}
P.S 要快速调试,您必须始终检查 browser console
以查找任何 JSON 异常。几乎 90% 的问题都通过显示的错误字符串得到解决。为了了解真正的问题,我做了同样的事情:)
我正在使用 for
循环在 HTML table 中使用 jquery
追加多行。
for
循环 (下面的代码) 在 ajax
调用的响应上运行,其中 returns 一个 array of arrays
.但是只有 1 行被添加到 table 而不管否。数组中的元素。
实际响应对象比较大,所以附上一个Google Drive link for a reference
如何附加 table 中的所有行?
代码片段(使用Jquery)
$(document).ready(function() {
$('#ghsubmitbtn').on('click', function(e) {
var data = JSON.stringify(JSONObj);
$.ajax({
url: 'https://api-crt.cert.havail.sabre.com/v4.2.0/shop/flights?mode=live&limit=50&offset=1',
method: 'POST',
contentType: "application/json; charset=utf-8",
data: data,
headers: {
"Authorization": 'Bearer ' + bat
},
success: function(data) {
var BookBtn = '<input type="submit" value="Book" name="book" class="btn btn-primary">';
for (var v = 0; v <= 999; v++) {
for (var b = 0; b <= 999; b++) {
for (var m = 0; m <= 999; m++) {
var row = $('<tr><td>' + data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].SequenceNumber + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalDateTime + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].DepartureAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].ArrivalAirport.LocationCode + '</td><td>' +
data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m].OperatingAirline.Code + '</td><td>' +
BookBtn + '</td></tr>');
$('#tblData').append(row);
}
}
}
}
});
});
问题
JSON Exception in the console for an undefined JSON property
分辨率
Safely Access Deeply Nested Values In JavaScript
Read this 文章,以避免将来出现 JSON 异常。
更正
if (data && data.OTA_AirLowFareSearchRS && data.OTA_AirLowFareSearchRS.PricedItineraries && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v] && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b] && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment && data.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary[v].AirItinerary.OriginDestinationOptions.OriginDestinationOption[b].FlightSegment[m]) {
// Build row using nested JSON
}
P.S 要快速调试,您必须始终检查 browser console
以查找任何 JSON 异常。几乎 90% 的问题都通过显示的错误字符串得到解决。为了了解真正的问题,我做了同样的事情:)