开发者

Switch javascript function

Im having trouble with a switch case statement, I've tried using if else too with no luck,

In my HTML I have

<select onBlur="functionCalc()" id="art">
<option value="hours" id="hours">Hours</option>
<option value="minutes" id="min开发者_C百科s">Minutes</option>
<option value="seconds" id="secs">Seconds</option>
</select>

and the js in relation to this is

// Workout if Average render time is in minutes seconds etc...
switch(art)
{
case minutes:
    document.write("Finally Friday"); return false;
    break;
case seconds:
    document.write("Finally seconds"); return false;
    break;
default:
    document.write("Finally mins"); return false;
}


Well hard to tell with the little code you gave, but if I had to guess you are not making string constants, so you were looking at the values of unused variables.

// Workout if Average render time is in minutes seconds etc...
switch(art)
{
case "minutes":
    document.write("Finally Friday"); return false;
    break;
case "seconds":
    document.write("Finally seconds"); return false;
    break;
default:
    document.write("Finally mins"); return false;
}


We can't see how you are setting the variable art so we can't make any suggestions there…

… unless you are assuming that art will be a global variable that references the current value of the form control with the same id (which it won't). You'd need do do something like: <select onBlur="functionCalc(this.options[this.selectedIndex].value)" and function functionCalc(art) { … } instead.

You can't use document.write in response to an event though. By the time the event fires, the document will have closed, so you'll automatically call document.open and wipe out all the existing content. Use DOM methods instead.

As @Hogan points out, you probably also need to explicitly make your case statements use strings too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜