隐藏基于文本的面板

时间:2015-10-16 05:09:20

标签: javascript

下面提到的脚本正常工作(1)但如果我把elseif条件不能正常工作(2)

我如何使用elseif条件

第1步

function cl_Indexchange(sender, eventArgs) {
        var pnl = document.getElementById("pnlSchedule");
        var item = eventArgs.get_item();
        if (item.get_text() == "Call Back Later (CBL)") {
            pnl.style.display = "block";
        } else

         {
            pnl.style.display = "none";
        }
    }

第2步

function cl_Indexchange(sender, eventArgs) {
            var pnl = document.getElementById("pnlSchedule");
            var item = eventArgs.get_item();
            if (item.get_text() == "Call Back Later (CBL)") {
                 pnl.style.display = "block";
            } else
               (item.get_text() == "Call Back After Explanation (CBE)") {
                    pnl.style.display = "block";
            }
            elseif
              (item.get_text() == "Call Back After Explanation (CBE)") {
                  pnl.style.display = "block";
            }elseif
            {
                 pnl.style.display = "none";
            }
     }

2 个答案:

答案 0 :(得分:0)

因为您在此if-else的结构不正确。 else将始终作为if的最后声明,else if之间的else将来if和{{1}之间需要提供空格如下所示:

if (item.get_text() == "Call Back Later (CBL)") {
   pnl.style.display = "block";
} 
else if(item.get_text() == "Call Back After Explanation (CBE)") {
   pnl.style.display = "block";
}
else
{
   pnl.style.display = "none";
}

此外,您可以合并else if部分,因为它做同样的事情,您可以使用单个ifelse声明:

if (item.get_text() == "Call Back Later (CBL)" || item.get_text() == "Call Back After Explanation (CBE)") {
   pnl.style.display = "block";
} 
else
{
   pnl.style.display = "none";
}

答案 1 :(得分:0)

您的if-else结构不合适。试试这个

function cl_Indexchange(sender, eventArgs) {
    var pnl = document.getElementById("pnlSchedule");
    var item = eventArgs.get_item();
    if (item.get_text() == "Call Back Later (CBL)") {
        pnl.style.display = "block";
    } else if (item.get_text() == "Call Back After Explanation (CBE)") {
        pnl.style.display = "block";
    }  else {
        pnl.style.display = "none";
    }
}