javascript 如何将 switch 语句放入另一个 switch 语句中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19584287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put a switch statement inside another switch statement
提问by grabury
Im trying to work out a quote value after a user makes a selection in 2 select boxes.
在用户在 2 个选择框中进行选择后,我试图计算出报价值。
It looks like 1 need 2 switch statements. Is this possible? (There are a lot more possible values than the ones below. I've deleted them for the sake of clarity)
看起来 1 需要 2 个 switch 语句。这可能吗?(可能的值比下面的要多得多。为了清楚起见,我已经删除了它们)
var workOutQuote = function() {
var value1 = $("#delivery_from_area").val();
var value2 = $("#delivery_to_area").val();
var val = '';
switch (value1)
{ case '1':
{ switch (value2)
{ case '1': val="90"; break; }
{ case '2': val="80"; break; }
{ case '3': val="70"; break; }
}
}
{ case '2':
{ switch (value2)
{ case '1': val="80"; break; }
{ case '2': val="90"; break; }
{ case '3': val="70"; break; }
}
}
}
Using the code above I get the error:
使用上面的代码我得到错误:
Uncaught SyntaxError: Unexpected token case
回答by Prateek
Use BREAK
, if you don't want to call all case
.
Otherwise ,you will get unexpected Results
使用BREAK
,如果你don't want to call all case
。否则,你会得到意想不到的结果
var workOutQuote = function() {
var value1 = $("#delivery_from_area").val();
var value2 = $("#delivery_to_area").val();
var val = '';
switch (value1){
case '1':
switch (value2){
case '1': val="90"; break;
case '2': val="80"; break;
case '3': val="70"; break;
}
break;
case '2':
switch (value2){
case '1': val="80"; break;
case '2': val="90"; break;
case '3': val="70"; break;
}
break;
}
回答by Bathsheba
The excessive use of braces is causing the statementafter the switch ()
(that javascript grammar mandates you to include) to end early. Adjust to this:
大括号的过度使用导致(javascript 语法要求您包含)之后的语句switch ()
提前结束。对此进行调整:
switch (value1){
case '1':
switch (value2){
case '1': val="90"; break;
case '2': val="80"; break;
case '3': val="70"; break;
}
break;
case '2':
switch (value2){
case '1': val="80"; break;
case '2': val="90"; break;
case '3': val="70"; break;
}
break;
}
I've also taken the liberty of putting a break;
between your outer cases since, at present your first case runs into the next one.
我也冒昧地break;
在你的外壳之间放了一个,因为目前你的第一个案例会进入下一个案例。
The final break
I added (right at the bottom) is there for clarity: many folk would not include this.
break
为了清楚起见,我添加的最后一个(在底部)是:很多人不会包括这个。
回答by Satpal
You are putting case in braces
, an example from your code { case '1': val="90"; break; }
您正在放入案例braces
,您的代码中的一个示例{ case '1': val="90"; break; }
var value1 = '2';
var value2 = '2';
var val = '';
switch (value1) {
case '1':
{
switch (value2) {
case '1':
val = "90";
break;
case '2':
val = "80";
break;
case '3':
val = "70";
break;
}
}
case '2':
{
switch (value2) {
case '1':
val = "80";
break;
case '2':
val = "90";
break;
case '3':
val = "70";
break;
}
}
}
Demo
演示
回答by Rémi Benoit
Don't put brackets around each case :
不要在每个案例周围放置括号:
switch (value1)
{ case '1':
{ switch (value2)
{ case '1': val="90"; break; }
{ case '2': val="80"; break; }
Should be :
应该 :
switch (value1)
{ case '1':
switch (value2) {
case '1': val="90"; break;
case '2': val="80"; break;
}
}