Adding a location to a javascript timetable
feel free to launch abuse at me if I have missed a question that expains this. I'm pretty new to JS aswell.
I'm building a timtable for shuttle buses to and from our offices. I wanted to add a new location.
My current code is this:
function changeTimes (selectedOption) {
var myTT = documen开发者_如何学编程t.getElementById("timeTable");
var myLocs = document.getElementById("Locations");
if (selectedOption=='1') {
myTT.innerHTML = brdepStr;
myLocs.innerHTML = 'Bath Road';
} else {
myTT.innerHTML = badepStr;
myLocs.innerHTML = 'Buckingham Avenue';
I then changed it to the following, which didn't work. Being a complete newbie to JS I'm guessing you can't have two "else" statements in a row?
function changeTimes (selectedOption) {
var myTT = document.getElementById("timeTable");
var myLocs = document.getElementById("Locations");
if (selectedOption=='1') {
myTT.innerHTML = brdepStr;
myLocs.innerHTML = 'Bath Road';
} else {
myTT.innerHTML = badepStr;
myLocs.innerHTML = 'Buckingham Avenue';
} else {
myTT.innerHTML = bwdepStr;
myLocs.innerHTML = 'Brunel Way' ;
}
Really appreciate any help. Thanks.
As @Mkilmanas said, there can be no 2 else
on the same if block (else will always be executed if all previous if/else if statements are not executed, so if you need 2 elses, you can just cut the code from the second else into the first else). Change the first else to else if(selectedOption == '2')
and you should be okay for this part :)
精彩评论