javascript:在switch case中使用条件 [英] javascript: using a condition in switch case

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

问题描述

抱歉这个愚蠢的问题。
如何在javascript switch-case语言元素中使用case的条件?
如下例所示,当变量 liCount 为< = 5且> 0时,情况应匹配;但是,我的代码不起作用:

Sorry for that dumb question. How can I use a condition for a case in the javascript switch-case language element? Like in the example below, a case should match when the variable liCount is <=5 and >0; however, my code does not work:

switch (liCount) {
    case 0:
        setLayoutState('start');
        var api = $('#UploadList').data('jsp');
        api.reinitialise();
        break;
    case (liCount<=5 && liCount>0):
        setLayoutState('upload1Row');
        var api = $('#UploadList').data('jsp');
        api.reinitialise();
        break;
    case (liCount<=10 && liCount>5):
        setLayoutState('upload2Rows');
        var api = $('#UploadList').data('jsp');
        api.reinitialise();
        break;
    case (liCount>10):
        var api = $('#UploadList').data('jsp');
        api.reinitialise();
        break;                  
}

感谢您的任何建议!

推荐答案

请参阅下面的 dmp的答案。如果可以的话,我会删除这个答案,但它被接受了,所以这是下一个最好的事情:)

See dmp's answer below. I'd delete this answer if I could, but it was accepted so this is the next best thing :)

你不能。 JS解释器要求您与switch语句进行比较(例如,没有case when when语句)。如果你真的想这样做,你可以只用 if(){..} else if(){..} blocks。

You can't. JS Interpreters require you to compare against the switch statement (e.g. there is no "case when" statement). If you really want to do this, you can just make if(){ .. } else if(){ .. } blocks.

这篇关于javascript:在switch case中使用条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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