问题是,一旦我在JSON对象中找到“日期”,我的for循环就会一直在滴答作响。我认为这导致我每次都进入条件的“Else”部分。
我是JavaScript / jQuery的新手。
来自控制台的输出
report-generation.js:443 Entering Else Loop for 02/13/2017. The date from the JSON file is 02/6/2017
report-generation.js:434 Entering If Loop for 02/13/2017. The date from the JSON file is 02/13/2017
report-generation.js:443 Entering Else Loop for 02/13/2017. The date from the JSON file is 02/20/2017
report-generation.js:488 02/6/2017 date from JSON. Selected Date is 02/13/2017
report-generation.js:488 02/13/2017 date from JSON. Selected Date is 02/13/2017
report-generation.js:488 02/20/2017 date from JSON. Selected Date is 02/13/2017
我的JavaScript代码
// Get Information Technology Division Manager Schedule
for (var i = 0; i < informationtechnologydivisionmanager.length; i++)
{
var emailAddressParts = informationtechnologydivisionmanager[i].name.split(' ');
var emailAddress = emailAddressParts[0] + "." + emailAddressParts[1] + "@example.com";
var createLink = "mailto:" + emailAddress + "?subject=Schedule for " + dateFromInput + "&body=This is a template. Please remove these lines and add your own";
if (informationtechnologydivisionmanager[i].date == theSelectedDate)
{
console.log("Entering If Loop for " + theSelectedDate + ". The date from the JSON file is " + informationtechnologydivisionmanager[i].date);
$('.informationtechnologydivisionmanagerName').html("<a href='" + createLink + "'>" + informationtechnologydivisionmanager[i].name + "</a><br>");
$('.informationtechnologydivisionmanagerTitle').html(informationtechnologydivisionmanager[i].title);
$('.informationtechnologydivisionmanagerMondaySchedule').html(informationtechnologydivisionmanager[i].mondayAM + "<br>" + informationtechnologydivisionmanager[i].mondayPM);
$('.informationtechnologydivisionmanagerTuesdaySchedule').html(informationtechnologydivisionmanager[i].tuesdayAM + "<br>" + informationtechnologydivisionmanager[i].tuesdayPM);
$('.informationtechnologydivisionmanagerWednesdaySchedule').html(informationtechnologydivisionmanager[i].wednesdayAM + "<br>" + informationtechnologydivisionmanager[i].wednesdayPM);
$('.informationtechnologydivisionmanagerThursdaySchedule').html(informationtechnologydivisionmanager[i].thursdayAM + "<br>" + informationtechnologydivisionmanager[i].thursdayPM);
$('.informationtechnologydivisionmanagerFridaySchedule').html(informationtechnologydivisionmanager[i].fridayAM + "<br>" + informationtechnologydivisionmanager[i].fridayPM);
} else {
console.log("Entering Else Loop for " + theSelectedDate + ". The date from the JSON file is " + informationtechnologydivisionmanager[i].date);
$('.informationtechnologydivisionmanagerName').html("<a href='" + createLink + "'>" + informationtechnologydivisionmanager[i].name + "</a><br>");
$('.informationtechnologydivisionmanagerTitle').html(informationtechnologydivisionmanager[i].title);
$('.informationtechnologydivisionmanagerMondaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set");
$('.informationtechnologydivisionmanagerTuesdaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set");
$('.informationtechnologydivisionmanagerWednesdaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set");
$('.informationtechnologydivisionmanagerThursdaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set");
$('.informationtechnologydivisionmanagerFridaySchedule').html("No Schedule Set" + "<br>" + "No Schedule Set");
}
}
MyHTML代码
<table id="informationtechnology" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th colspan="6" class="caption">Information Technology</th>
</tr>
<tr>
<th><span>Name</span></th>
<th><span>Monday</span></th>
<th><span>Tuesday</span></th>
<th><span>Wednesday</span></th>
<th><span>Thursday</span></th>
<th><span>Friday</span></th>
</tr>
</thead>
<tbody>
<tr>
<td class="lalign"><span class="informationtechnologydivisionmanagerName"></span><span class="informationtechnologydivisionmanagerTitle" style="color: navy;text-align: center;"></span></td>
<td><span class="informationtechnologydivisionmanagerMondaySchedule"></span></td>
<td><span class="informationtechnologydivisionmanagerTuesdaySchedule"></span></td>
<td><span class="informationtechnologydivisionmanagerWednesdaySchedule"></span></td>
<td><span class="informationtechnologydivisionmanagerThursdaySchedule"></span></td>
<td><span class="informationtechnologydivisionmanagerFridaySchedule"></span></td>
</tr>
</tbody>
</table>
答案 0 :(得分:2)
用于退出循环的两个主要关键字是break
和continue
break
完全停止循环并在循环后跳转到该行
continue
只是停止当前迭代,然后跳转到循环的开头,继续下一次迭代。
所以你需要的是break