问题描述
我在编写的一些JavaScript中遇到问题,其中Switch语句似乎没有按预期工作。
I have problem in some JavaScript that I am writing where the Switch statement does not seem to be working as expected.
switch (msg.ResultType) {
case 0:
$('#txtConsole').val("Some Val 0");
break;
case 1:
$('#txtConsole').val("Some Val 1");
break;
case 2:
$('#txtConsole').text("Some Val 2");
break;
}
ResultType是0-2的整数值,我可以在FireBug中看到。在所有情况下,交换机将控制权转移到最终的中断语句,这意味着完全跳过所有逻辑。我缺少什么?
The ResultType is an integer value 0-2 and I can see that in FireBug. In all cases, the switch transfers control to the final break statement which means all the logic is completely skipped. What am I missing?
推荐答案
我确信一个开关在Actionscript中使用===进行比较,因为JS和AS两者都遵循ECMAScript标准,我猜这同样适用于JS。我的猜测是该值实际上不是数字,但可能是一个字符串。
I'm sure that a switch uses === for comparison in Actionscript and since JS and AS both follow the ECMAScript standard, I guess the same applies to JS. My guess is that the value is not actually a Number, but perhaps a String.
您可以尝试在交换机中使用parseInt(msg.ResultType)或使用字符串案件。
You could try to use parseInt(msg.ResultType) in the switch or use strings in the cases.
这篇关于JavaScript切换语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!