下面的代碼應該從一個cookie中讀取存儲的ID(保存在一個cookie中,所以如果你重新加載頁面,它會讓你保存的會話存儲在一個cookie中,以便下次閱讀) ,查找鏈接到該ID的DIV,然後將行附加到包含ID的會話的標題,日期,代碼和時間的表中,但是當有多個日期鏈接到單個會話DIV時,它應創建一個爲每個行,這似乎是做得很對。看起來像我的每個循環不止一次循環
但是,似乎每個循環在sessDateVar.each(function(i, val)
運行兩次?當我console.log
當前會話代碼和日期在while循環中被追加的行,以及i
的值似乎重複?我似乎無法弄清楚這一點。
現在的代碼有點亂,對不起!如果我遺漏了任何東西,請告訴我!
我的控制檯的內容時,while循環運行
(index):1994 Wrote a row for session A6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session D6 on date Thursday, May 19, 2016
(index):1995 1
(index):1994 Wrote a row for session A6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session D6 on date Thursday, May 19, 2016
(index):1995 1
(index):1994 Wrote a row for session C6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session F6 on date Thursday, May 19, 2016
(index):1995 1
(index):1994 Wrote a row for session C6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session F6 on date Thursday, May 19, 2016
(index):1995 1
守則
function writeTable() {
if (checkCookie() === false) {
$('#table-container').append('<table class="tg" id="session-table">\n<thead>\n<tr>\n<th class="tg-yw4l">Session</th>\n<th class="tg-yw4l">Date</th>\n<th class="tg-yw4l">Time</th>\n<th class="tg-yw4l"></th>\n</tr>\n</thead>\n<tbody>\n</tbody>\n</table>');
$('.table-container-export').append('<table class="tg" cellpadding="10" id="session-table-export">\n<thead>\n<tr>\n<th class="tg-title">Session</th>\n<th class="tg-info">Date</th>\n<th class="tg-info">Time</th>\n<th class="tg-info">Session Code</th>\n</tr>\n</thead>\n<tbody>\n</tbody>\n</table>');
} else if (checkCookie() === true) {
var askToLoad = confirm('You have saved sessions in your browser, would you like to load those sessions?');
if (askToLoad === true) {
$('#table-container').append('<table class="tg" id="session-table">\n<thead>\n<tr>\n<th class="tg-yw4l">Session</th>\n<th class="tg-yw4l">Date</th>\n<th class="tg-yw4l">Time</th>\n<th class="tg-yw4l"></th>\n</tr>\n</thead>\n<tbody>\n</tbody>\n</table>');
$('.table-container-export').append('<table class="tg" cellpadding="10" id="session-table-export">\n<thead>\n<tr>\n<th class="tg-title">Session</th>\n<th class="tg-info">Date</th>\n<th class="tg-info">Time</th>\n<th class="tg-info">Session Code</th>\n</tr>\n</thead>\n<tbody>\n</tbody>\n</table>');
var tableDataUnparsed = Cookies.get('session-table');
var tableDataExportUnparsed = Cookies.get('session-table-export');
var tableDataParsed = JSON.parse(tableDataUnparsed);
var tableDataExportParsed = JSON.parse(tableDataExportUnparsed);
var tableDataExport = tableDataExportParsed;
$.each(tableDataExportParsed, function(i, val){
var sessID = val;
var sessionToFind = $(sessID);
var sessTitle = sessionToFind.find('.title').html();
var sessDateVar = sessionToFind.find('.date');
if (sessDateVar.size() > 1) {
var sessDates = [];
sessDateVar.each(function() {
sessDates.push($.trim($(this).html()));
});
} else {
var sessDate = $.trim(sessionToFind.find('.date').html());
var sessDates = false;
}
var sessTimeVar = sessionToFind.find('.time');
if (sessTimeVar.size() > 1) {
var sessTimes = [];
sessTimeVar.each(function() {
sessTimes.push($.trim($(this).html()));
});
} else {
var sessTime = $.trim(sessionToFind.find('.time').html());
}
var sessCodeVar = sessionToFind.find('.code');
if (sessCodeVar.size() > 1) {
var sessCodes = [];
sessCodeVar.each(function() {
sessCodes.push($.trim($(this).html()));
});
} else {
var sessCode = $.trim(sessionToFind.find('.code').html());
}
if (sessDates === false) {
$('#session-table-export').append('<tr data-sessionID=' + sessID + '>\n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;"> ' + sessTitle + '</span></td>\n<td width="100px" style="width:100px" class="tg-info" > ' + sessDate + '</td>\n<td width="100px" style="width:100px" class="tg-info"> ' + sessTime + '</td>\n<td width="50px" style="width:50px" class="tg-sessioncode">' + sessCode + '</td>\n</tr>\n');
} else {
sessDateVar.each(function(i, val){
$('#session-table-export').append('<tr data-sessionID=' + sessID + '>\n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;">' + sessTitle + '</span></td>\n<td width="100px" style="width:100px" class="tg-info" > ' + sessDates[i] + '</td>\n<td width="100px" style="width:100px" class="tg-info">' + sessTimes[i] + '</td>\n<td width="50px" style="width:50px" class="tg-sessioncode"><p>' + sessCodes[i] + '</p></td></tr>\n');
});
}
});
loadedFromCookie = true;
} else if (askToLoad === false) {
Cookies.remove('session-table', {
path: '/isotope'
});
Cookies.remove('session-table-export', {
path: '/isotope'
});
$('#table-container').append('<table class="tg" id="session-table">\n<thead>\n<tr>\n<th class="tg-yw41">Session</th>\n<th class="tg-yw4l">Date</th>\n<th class="tg-yw4l">Time</th>\n<th class="tg-yw4l">\n</th>\n</tr>\n</thead>\n<tbody>\n</tbody>\n</table>');
$('.table-container-export').append('<table class="tg" cellpadding="10" id="session-table-export">\n<thead>\n<tr>\n<th class="tg-title">Session</th>\n<th class="tg-info">Date</th>\n<th class="tg-info">Time</th>\n<th class="tg-info">Session Code</th>\n</tr>\n</thead>\n<tbody>\n</tbody>\n</table>');
}
}
};
writeTable();
有問題的循環
if (sessDates === false) {
$('#session-table-export').append('<tr data-sessionID=' + sessID + '>\n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;"> ' + sessTitle + '</span></td>\n<td width="100px" style="width:100px" class="tg-info" > ' + sessDate + '</td>\n<td width="100px" style="width:100px" class="tg-info"> ' + sessTime + '</td>\n<td width="50px" style="width:50px" class="tg-sessioncode">' + sessCode + '</td>\n</tr>\n');
} else {
sessDateVar.each(function(i, val){
$('#session-table-export').append('<tr data-sessionID=' + sessID + '>\n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;">' + sessTitle + '</span></td>\n<td width="100px" style="width:100px" class="tg-info" > ' + sessDates[i] + '</td>\n<td width="100px" style="width:100px" class="tg-info">' + sessTimes[i] + '</td>\n<td width="50px" style="width:50px" class="tg-sessioncode"><p>' + sessCodes[i] + '</p></td></tr>\n');
console.log('Wrote a row for session ' + sessCodes[i] + ' on date ' + sessDates[i] + '');
console.log(i);
});
}
您是否在循環前嘗試驗證sessDateVar內容? –
我做了,並且實際上我只是使用.each()作爲while循環的替代方法(以便它只會循環多次,因爲有附加到該會話的日期)。但即使我用while循環替換.each(),我也遇到了同樣的問題。 –
你能告訴我sessDateVar –