如果/ Else如果声明减少

时间:2014-03-15 18:47:20

标签: javascript jquery if-statement

从我的服务器,我得到一个正在运行的“时间轴”的XML列表。 当时间轴处于“正在运行”或“结束时保持”状态时,按钮将处于活动状态。

我已经这样做了,它的确有效。 但我想在我的代码中拒绝“Else”语句。 有什么想法吗?

 function statusCheck()
{
    $.ajax({
        type: "GET",
        url: "/query/timelineStatus?id=1-30",
        dataType: "xml",
        success: function (xml) {
            $(xml).find("timelineStatus").each(function(){
                var timelineId = parseInt($(this).attr("id"));
                var playState = $(this).find("playState").text();
                if (timelineId == 1) // timeline_1
                {
                    changeJQMThemeSwatch("#timeline_1", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }
                else if (timelineId == 2) // timeline_2
                {
                    changeJQMThemeSwatch("#timeline_2", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }
                else if (timelineId == 3) // timeline_3
                {
                    changeJQMThemeSwatch("#timeline_3", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }
                else if (timelineId == 4) // timeline_4
                {
                    changeJQMThemeSwatch("#timeline_4", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }
                else if (timelineId == 5) // timeline_5
                {
                    changeJQMThemeSwatch("#timeline_5", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }
                else if (timelineId == 6) // timeline_6
                {
                    changeJQMThemeSwatch("#timeline_6", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }

        }
    })
}

3 个答案:

答案 0 :(得分:1)

根据id命名的标准,你可以使用一个小的字符串连接。

function statusCheck()
{
    $.ajax({
        type: "GET",
        url: "/query/timelineStatus?id=1-30",
        dataType: "xml",
        success: function (xml) {
            $(xml).find("timelineStatus").each(function(){
                var timelineId = parseInt($(this).attr("id"));
                var playState = $(this).find("playState").text();
                if (timelineId > 0 && timelineId <= 6) {
                    changeJQMThemeSwatch("#timeline_" + timelineId, (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }
            });
        }
    });
}

答案 1 :(得分:1)

动态地做什么呢?

changeJQMThemeSwatch("#timeline_"+timelineId, (playState == "Running" || playState == "Held at end") ? "b" : "a");

答案 2 :(得分:0)

如果您的所有#timeline_x值都遵循此模式,请执行以下操作:

function statusCheck()
{
    $.ajax({
        type: "GET",
        url: "/query/timelineStatus?id=1-30",
        dataType: "xml",
        success: function (xml) {
            $(xml).find("timelineStatus").each(function(){
                var timelineId = parseInt($(this).attr("id"));
                var playState = $(this).find("playState").text();
                if(timelineId > 0 && timelineId <= 6) { // change as needed
                     changeJQMThemeSwatch("#timeline_" + timelineId, (playState == "Running" || playState == "Held at end") ? "b" : "a");
                }
            }
        })
    });
}

如果您需要对某些值使用不同的逻辑,请改用switch

function statusCheck()
{
    $.ajax({
        type: "GET",
        url: "/query/timelineStatus?id=1-30",
        dataType: "xml",
        success: function (xml) {
            $(xml).find("timelineStatus").each(function(){
                var timelineId = parseInt($(this).attr("id"));
                var playState = $(this).find("playState").text();
                switch(timelineId) {
                    case 1:
                        changeJQMThemeSwatch("#timeline_1", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                    break;
                    case 2:
                        changeJQMThemeSwatch("#timeline_2", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                    break;
                    case 3:
                        changeJQMThemeSwatch("#timeline_3", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                    break;
                    case 4:
                        changeJQMThemeSwatch("#timeline_4", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                    break;
                    case 5:
                        changeJQMThemeSwatch("#timeline_5", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                    break;
                    case 6:
                        changeJQMThemeSwatch("#timeline_6", (playState == "Running" || playState == "Held at end") ? "b" : "a");
                    break;
                }
            }
        })
    });
}