JS里的switch怎么写?

qingyingfei 2012-09-26 11:25:40
// switch(temp.message.List[i].State)
/// {
// case 0:
// orderstate="未付款";
// break;
// case 1:
// orderstate="未发货";
// break;
// case 2:
//// orderstate="未收货";
// break;
// case 3:
// orderstate="已收货";
// break;
//
// }


if(temp.message.List[i].State==0)
orderstate="未付款";
else if(temp.message.List[i].State==1)
orderstate="未发货";
else if(temp.message.List[i].State==2)
orderstate="未收货";
else
orderstate="已收货";


temp.message.List[i].State字段在数据库里为int 型,我这个switch语句为什么不执行啊~~~用if ,else语句就好的很……真是无语了,哪位大侠不吝赐教,小女子不胜感激!!!
...全文
440 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Alex20150716 2012-09-26
  • 打赏
  • 举报
回复
switch( event.keyCode ){
case 69: doanything

case 72: doanything

case 73:doanything
default:
break;
}
未知数 2012-09-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
谢谢各位大侠,我一直搞不清楚JS里的变量是什么类型,藏得太深了,都不让我定义类型……
[/Quote]
用typeof可以获得
但这其实是js灵活性的体现,用熟了会觉得越加舒服,写参数,调函数,返回值时都不用考虑参数和返回值的类型,函数可以接收多种类型参数,或根据参数返回不同类型的结果
qingyingfei 2012-09-26
  • 打赏
  • 举报
回复
谢谢各位大侠,我一直搞不清楚JS里的变量是什么类型,藏得太深了,都不让我定义类型……
  • 打赏
  • 举报
回复
// switch(parseInt(temp.message.List[i].State))
/// {
// case 0:
// orderstate="未付款";
// break;
// case 1:
// orderstate="未发货";
// break;
// case 2:
//// orderstate="未收货";
// break;
// case 3:
// orderstate="已收货";
// break;
//
// }
scscms太阳光 2012-09-26
  • 打赏
  • 举报
回复
var id=temp.message.List[i].State;//id-0 强制转数字
switch(id-0)
{
case 0:
orderstate="未付款";
break;
case 1:
orderstate="未发货";
break;
case 2:
orderstate="未收货";
break;
case 3:
orderstate="已收货";
break;
}
alert(orderstate);
abc0824 2012-09-26
  • 打赏
  • 举报
回复
switch(parseInt(temp.message.List[i].State))
{
case 0:
orderstate="未付款";
break;
case 1:
orderstate="未发货";
break;
case 2:
orderstate="未收货";
break;
case 3:
orderstate="已收货";
break;

}

要转成整形的。
未知数 2012-09-26
  • 打赏
  • 举报
回复
目测 原因就是 temp.message.List[i].State是字符串导致的

而if可以成功是因“==”不检测类型,"0"==0是为true的,===才会检测类型是否相同,"0"===0为false
未知数 2012-09-26
  • 打赏
  • 举报
回复
在数据库是int,到了页面可未必了
你试试
alert(typeof temp.message.List[i].State);
看看究竟是数值还是字符串,如果是string
就把case后的数字加上引号
szandyyu 2012-09-26
  • 打赏
  • 举报
回复
记得加break 很重要
lilo4033 2012-09-26
  • 打赏
  • 举报
回复

switch(~~temp.message.List[i].State){
....
}

qq517995084 2012-09-26
  • 打赏
  • 举报
回复

switch(parseInt(temp.message.List[i].State))
{
case 0:
orderstate="未付款";
break;
case 1:
orderstate="未发货";
break;
case 2:
orderstate="未收货";
break;
case 3:
orderstate="已收货";
break;
default: return("invalid");
break;

}



  • 打赏
  • 举报
回复
呵呵,接了贴,你的分数估计也不多
  • 打赏
  • 举报
回复
记得结贴哦

87,919

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧