javascript开关(true) [英] javascript switch(true)

查看:141
本文介绍了javascript开关(true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试处理ajax json回复

Hi i am trying to handle an ajax json response

这是我的代码

success: function (j) {     
    switch(true)
    {
        case (j.choice1):

            alert("choice2");
        break;
        case (j.choice2):
                alert("choice2");
        break;
        default:
            alert("default");
        break;
    }
}

基于j返回我做我的行动但是我一直得到默认值。

based on what j is return i do my action BUT i keep getting the default.

我已经警告j值并且正确。有些情况(j.choice1)case(j.choice2)无法正常工作。

I have alert the j values and come correct.Some how case (j.choice1) case (j.choice2) is not working.

我试过案例(j.choice1!=)(j.choice2!=)但在这种情况下我一直是第一选择。

I tried case (j.choice1!="") (j.choice2!="") But in this scenario i keep getting the first choice.

我缺少什么

推荐答案

它对我有用:

var a = 0, b = true;

switch(true) {
    case a:
        alert('a');
        break;
    case b:
        alert('b');
        break;
}

然而,案例标签必须等于 true ,而不是jut隐式为true。

另外,只有第一个计算结果为 true的情况将执行。

However, the case labels must be equal to true, not jut implicitly true.
Also, only the first case that evaluates to true will execute.

这篇关于javascript开关(true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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