Javascript中的case语句 [英] case statements in Javascript

查看:77
本文介绍了Javascript中的case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些代码如下:


if(document.Insurance.State.selectedIndex == 1)

{

ifIll();

}

else if(document.Insurance.State.selectedIndex == 2)

{

elseKan();

}

else if(document.Insurance.State.selectedIndex == 3)

{

elseInd();

}


我试图用case语句替换if-else语句为

如下:


var index = document.Insurance.State.selectedIndex;


开关(索引)

{

案例1:

ifIll()

休息

案例2:

elseKan()

休息

案例3:elseInd()

休息

}


这段代码不起作用!我在这里错过了什么吗?谢谢

So I have some code like:

if (document.Insurance.State.selectedIndex == 1)
{
ifIll();
}
else if (document.Insurance.State.selectedIndex == 2)
{
elseKan();
}
else if (document.Insurance.State.selectedIndex == 3)
{
elseInd();
}

I am trying to replace the if-else statements with case statement as
follows:

var index = document.Insurance.State.selectedIndex;

switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break
}

This code doesn''t work ! Am I missing something here? Thanks

推荐答案

4月16日下午4:41,Navodit < kaush ... @ uiuc.eduwrote:
On Apr 16, 4:41 pm, "Navodit" <kaush...@uiuc.eduwrote:

所以我有一些代码如下:


if(document。 Insurance.State.selectedIndex == 1)

{

ifIll();}


else if(document.Insurance。 State.selectedIndex == 2)

{

elseKan();}


else if(document.Insurance.State。 selectedIndex == 3)

{

elseInd();


}


我试图用case语句替换if-else语句,因为

如下:


var index = document.Insurance.State.selectedIndex;


开关(索引)

{

案例1:

ifIll()

休息

案例2:

elseKan()

休息

案例3:elseInd()< br $> b $ b休息


}


此代码无效!我在这里错过了什么吗?谢谢
So I have some code like:

if (document.Insurance.State.selectedIndex == 1)
{
ifIll();}

else if (document.Insurance.State.selectedIndex == 2)
{
elseKan();}

else if (document.Insurance.State.selectedIndex == 3)
{
elseInd();

}

I am trying to replace the if-else statements with case statement as
follows:

var index = document.Insurance.State.selectedIndex;

switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break

}

This code doesn''t work ! Am I missing something here? Thanks



我自己也看不到问题。我建议你要么得到一个好的调试器(Firebug for Firefox很棒),或者你添加了一个

默认值:case来捕获掉落的任何东西这个案例,

可以提醒哪些值传递给开关盒,如果

any。此外,在某处使用一些try / catch子句可能很有用

,这样你就会收到任何错误的通知。


一切顺利。


Daz。

I can''t see the problem myself. I would recommend you either get a
good debugger (Firebug for Firefox is awesome), or that you add a
default: case to catch anything that falls off the end of the case,
which can alert as to what value was passed to the switch case, if
any. Also, it might be useful to use some try/catch clauses in there
somewhere, that way you will be notified of any errors.

All the best.

Daz.


4月16日下午4:41,Navodit < kaush ... @ uiuc.eduwrote:
On Apr 16, 4:41 pm, "Navodit" <kaush...@uiuc.eduwrote:

所以我有一些代码如下:


if(document。 Insurance.State.selectedIndex == 1)

{

ifIll();}


else if(document.Insurance。 State.selectedIndex == 2)

{

elseKan();}


else if(document.Insurance.State。 selectedIndex == 3)

{

elseInd();


}


我试图用case语句替换if-else语句,因为

如下:


var index = document.Insurance.State.selectedIndex;


开关(索引)

{

案例1:

ifIll()

休息

案例2:

elseKan()

休息

案例3:elseInd()< br $> b $ b休息


}


此代码无效!我在这里错过了什么吗?谢谢
So I have some code like:

if (document.Insurance.State.selectedIndex == 1)
{
ifIll();}

else if (document.Insurance.State.selectedIndex == 2)
{
elseKan();}

else if (document.Insurance.State.selectedIndex == 3)
{
elseInd();

}

I am trying to replace the if-else statements with case statement as
follows:

var index = document.Insurance.State.selectedIndex;

switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break

}

This code doesn''t work ! Am I missing something here? Thanks



哦,另外你要注意的是,你要传递给switch case的

参数的类型。你可以传递一个

字符串而不是一个整数,在这种情况下你切换案例应该是

看起来像这样:


开关(索引){


case" 1":

ifIll();

break;


case" 2":

elseKan();

break;


case" 3":

elseInd();

休息;

}


不是这样:


开关(索引){


案例1:

ifIll();

休息;


案例2:

elseKan();

休息;


案例3:

elseInd();

休息;

}


case(没有双关语意),你应该使用一个默认情况

抓住掉落在案件中的任何东西,除非你特意

希望忽略它们。

Oh, and another thing you should be aware of, is the type of the
argument you are passing to the switch case. You could be passing a
string instead of an integer, in which case you switch case should
look like this:

switch (index) {

case "1":
ifIll();
break;

case "2":
elseKan();
break;

case "3":
elseInd();
break;
}

NOT this:

switch (index) {

case 1:
ifIll();
break;

case 2:
elseKan();
break;

case 3:
elseInd();
break;
}

In either case (no pun intended), you should use a default case to
catch anything that drops through the case, unless you specifically
want them to be ignored.


Navodit于2007年4月16日在comp.lang.javascript中写道
Navodit wrote on 16 apr 2007 in comp.lang.javascript:

所以我有一些代码如下:


if(document.Insurance.State.selectedIndex == 1)

{

ifIll();
}

else if(document.Insurance.State.selectedIndex == 2)

{

elseKan();
}

else if(document.Insurance.State.selectedIndex == 3)

{

elseInd();
}

我试图用case语句替换if-else语句,因为

如下:


var index = document.Insurance.State.selectedIndex;


开关(索引)

{

案例1:

ifIll()

休息

案例2:

elseKan( )

休息

案例3:elseInd()

休息
}


这段代码不起作用!我在这里错过了什么吗?谢谢
So I have some code like:

if (document.Insurance.State.selectedIndex == 1)
{
ifIll();
}
else if (document.Insurance.State.selectedIndex == 2)
{
elseKan();
}
else if (document.Insurance.State.selectedIndex == 3)
{
elseInd();
}

I am trying to replace the if-else statements with case statement as
follows:

var index = document.Insurance.State.selectedIndex;

switch (index)
{
case 1:
ifIll()
break
case 2:
elseKan()
break
case 3: elseInd()
break
}

This code doesn''t work ! Am I missing something here? Thanks



可能你做了,因为这样可以正常工作:


============ ============================

< script type =''text / javascript''>

函数ch(x){

开关(x.selectedIndex){

案例1:

alert( ''one'');

休息;

案例2:

alert(''two'');

休息;

案例3:

提醒(''三'');

休息;

};

};

< / script>


< select onchange =''ch(this)' '>

<选项 -

< option1

< option2

< option3

< / select>

============================== ==========


-

Evertjan。

荷兰。

(请将我的电子邮件地址中的x'变为点数)

Probably you did, as this works fine:

========================================
<script type=''text/javascript''>
function ch(x){
switch (x.selectedIndex) {
case 1:
alert(''one'');
break;
case 2:
alert(''two'');
break;
case 3:
alert(''three'');
break;
};
};
</script>

<select onchange=''ch(this)''>
<option--
<option1
<option2
<option3
</select>
========================================


--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


这篇关于Javascript中的case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆