Javascript 排除if语句中的所有其他活动ID

Javascript 排除if语句中的所有其他活动ID,javascript,if-statement,redirect,conditional,Javascript,If Statement,Redirect,Conditional,我正在编写一个带有if语句的简单函数,根据用户的选择重定向用户。基本上,用户可以看到几个选项(a、B、C等)。当选择A和B时,用户被重定向到一个URL,当选择A和C时,用户被重定向到另一个URL 到目前为止,这是可行的,使用以下代码: function redirect() { if (((document.getElementById('A').classList.contains('active')) == true) && ((document.getElement

我正在编写一个带有
if
语句的简单函数,根据用户的选择重定向用户。基本上,用户可以看到几个选项(a、B、C等)。当选择A和B时,用户被重定向到一个URL,当选择A和C时,用户被重定向到另一个URL

到目前为止,这是可行的,使用以下代码:

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

再次感谢您,如果这个问题反映了我对编码的基本知识,那么很抱歉。

您可以存储所有的is活动值:

然后:

if( state.A && state.B)
可以存储所有is活动值:

然后:

if( state.A && state.B)

列出
处于活动状态的所有元素,并循环遍历它们

// Get a list of all elements having the "active" class
var activeElements = document.querySelectorAll(".active");
var redirect = true; 

// Loop through all the elements having the active class
// and set redirect to false if one of them doesn't have one of the following ID: 'A' or 'B'
for (var i =0; i<activeElements.length;i++) {
    if(activeElements[i].id != 'A' && activeElements[i].id != 'B') {
        redirect = false;
    }
} 

//If the redirect variable is still true
if(redirect){
    redirect();
}

列出
处于活动状态的所有元素,并循环遍历它们

// Get a list of all elements having the "active" class
var activeElements = document.querySelectorAll(".active");
var redirect = true; 

// Loop through all the elements having the active class
// and set redirect to false if one of them doesn't have one of the following ID: 'A' or 'B'
for (var i =0; i<activeElements.length;i++) {
    if(activeElements[i].id != 'A' && activeElements[i].id != 'B') {
        redirect = false;
    }
} 

//If the redirect variable is still true
if(redirect){
    redirect();
}

你需要用
map
every
some
方法明确地检查一些东西。为什么不干脆
var a=(document.getElementById('a').classList.contains('active'))
b
c
类似呢。然后你检查
如果(a和&b)
等你需要用
映射
每一个
一些
方法明确地检查一些东西。为什么不干脆
变量a=(document.getElementById('a')。classList.contains('active'))
和类似的
b
c
。然后检查
if(a&&b)
etc,这将如何排除所有其他ID?为什么不直接使用
Array#map
Array#reduce
?这将如何排除所有其他ID?为什么不直接使用
Array#map
Array#reduce
?我想你的意思是(使用s)为什么不直接使用
var arr=[“A”、“B”、“C”].filter(id=>document.getElementById(id).classList.contains(“active”)
var arr=Array.from(document.queryselectoral(“.active”)).map(elem=>elem.id)
&
链也可以用
数组#every
替换。非常感谢,请您解释一下?arr.push方法的作用以及为什么它会被设置为false?@Andre
?:
是三元运算符。似乎有效,Sugumar的答案和Xufox的替代方法再次感谢您!我想你的意思是(用s)为什么不干脆
vararr=[“A”,“B”,“C”].filter(id=>document.getElementById(id).classList.contains(“active”)
var arr=Array.from(document.queryselectoral(“.active”)).map(elem=>elem.id)
&
链也可以用
数组#every
替换。非常感谢,请您解释一下?arr.push方法的作用以及为什么它会被设置为false?@Andre
?:
是三元运算符。似乎有效,Sugumar的答案和Xufox的替代方法再次感谢您!
var arr = [];
var A = (document.getElementById('A').classList.contains('active')) == true)?arr.push('A'):false;
var B = (document.getElementById('B').classList.contains('active')) == true)?arr.push('B'):false;
var C = (document.getElementById('C').classList.contains('active')) == true)?arr.push('C'):false;

if(arr.includes('A') && arr.includes('B') && arr.length ==2){

}