首页 > 解决方案 > 如果单击多个复选框,则打开多个选项卡

问题描述

我正在设计一个 chrome 扩展,用户可以选择检查任意数量的框,这些框将加载与他们选择的选择相对应的不同网页。

到目前为止,我的复选框的 html 代码是:

<input type="checkbox" name="selection" value="google" 
id="google" >
<input type="checkbox" name="selection" value="yahoo" 
id="yahoo" >
<input type="checkbox" name="selection" value="msn" 
id="msn" >

我尝试使用的JS是这样的:

function redirect()
{ 
if(document.getElementById("google").checked == true) . 
window.location.href = 'https://www.google.com';

else if(document.getElementById("yahoo").checked==true)
window.location.href = 'https://www.yahoo.com;

else if(document.getElementById("msn").checked == true) 
window.location.href = 'https://www.msn.com;'
}

如果我选择了一个选项,则站点加载正确,但如果选中了多个,则不会将所有站点加载到每个选中的复选框。如何更改代码,以便如果选中多个复选框,该复选框的每个站点都会加载?

标签: javascripthtml

解决方案


if (document.getElementById("google").checked == true)
    window.open('https://www.google.com');
else if (document.getElementById("yahoo").checked == true)
    window.open('https://www.yahoo.com');
else if (document.getElementById("msn").checked == true)
    window.open('https://www.msn.com');

推荐阅读