【发布时间】:2017-09-19 13:56:28
【问题描述】:
我正在编写一个带有if 语句的简单函数,根据用户的选择重定向用户。基本上,用户会看到几个选项(A、B、C 等)。 and the user is redirect to one URL when say A and B are selected, and to another URL when say A and C are selected.
到目前为止,这是可行的,使用以下代码:
function redirect() {
if (((document.getElementById('A').classList.contains('active')) == true) && ((document.getElementById('B').classList.contains('active')) == true)) {
window.location = "https://www.example.com";
};
};
我的问题是我还希望能够排除所有其他可能性,这意味着如果选择了 A 和 B,我想添加一个重定向用户的子句,但没有其他选项。有没有办法在不明确将所有其他选项列为('active')) == false 的情况下做到这一点?
再次感谢您,如果这个问题反映了我对编码的基本知识,我们深表歉意。
【问题讨论】:
-
你需要明确检查
-
尝试使用
map、every和someArray方法。 -
为什么不只是
var a = (document.getElementById('A').classList.contains('active'))和b和c类似。然后你检查if (a && b)等
标签: javascript if-statement redirect conditional