我不确定是否可以在一个方法中打开多个选项卡/窗口,因为 window.open 将停止当前 JavaScript 方法的运行。 可以打开多个窗口,但是有一个问题。您必须启用弹出窗口,大多数人默认禁用。然后是浏览器问题:作为开发人员,您无法决定浏览器是打开一个新选项卡还是一个新窗口。您可以做什么来显示多个选项卡/窗口,是使用我在答案底部复制的一段 JavaScript。但请注意,虽然它可以工作,但它只打开一个选项卡,其余的是新的“弹出”窗口(至少在 Chrome 中)。
如果您需要打开一页,此代码确实有效,但只打开一页。
使用 jQuery 可能会让您的生活更轻松:
HTML
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"> </script>
<script type="text/javascript" src="boxes.js"></script>
</head>
<body>
<input type="checkbox" name="box1" value="box1" id="box1"><label>box1</label><br/>
<input type="checkbox" name="box2" value="box2" id="box2"><label>box2</label><br/>
<input type="checkbox" name="box3" value="box3" id="box3"><label>box3</label><br/>
<input type="checkbox" name="box4" value="box4" id="box4"><label>box4</label><br/>
<input type="button" value="Submit" name="submitButton" onclick="submit()">
</body>
</html>
JavaScript (jQuery)
function submit() {
var box1 = $('#box1');
var box2 = $('#box2');
var box3 = $('#box3');
var box4 = $('#box4');
if (box1.prop('checked')) {
goToUrl('http://box1url.com');
//Redirect by box1
}
if (box2.prop('checked')) {
goToUrl('http://box2url.com');
//Redirect by box2
}
if (box3.prop('checked')) {
goToUrl('http://box3url.com');
//Redirect by box3
}
if (box4.prop('checked')) {
goToUrl('http://box4url.com');
//Redirect by box4
}
}
function goToUrl(url) {
var win = window.open(url, '_blank');
win.focus();
}
编辑:没有 JQUERY
如果你决定不使用 jQuery,你可以将它用于 JavaScript:
function submit() {
var box1 = document.getElementById('box1');
var box2 = document.getElementById('box2');
var box3 = document.getElementById('box3');
var box4 = document.getElementById('box4');
if (box1.checked) {
goToUrl('http://box1url.com');
//Redirect by box1
}
if (box2.checked) {
goToUrl('http://box2url.com');
//Redirect by box2
}
if (box3.checked) {
goToUrl('http://box3url.com');
//Redirect by box3
}
if (box4.checked) {
goToUrl('http://box4url.com');
//Redirect by box4
}
}
function goToUrl(url) {
var win = window.open(url, '_blank');
win.focus();
}
EDIT2:添加了“多网址”
var urls = []; //Initialize empty array
if (box1.checked) {
urls.push('http://box1url.com'); // If box is checked, push the url
}
urls.forEach(function(url) {
goToUrl(url); //Go to all URLs in a for-each loop
});
EDIT3:按值添加了更多“动态”网址
如果将 URL 设置为复选框的值,您可以随后在 JavaScript 中检索它:
HTML:
<input type="checkbox" name="box1" value="http://box1url.com" id="box1"><label>box1</label><br/>
JavaScript(无 jQuery):
if (box1.checked) {
urls.push(box1.value);
//Redirect by box1
}
JavaScript (jQuery):
if (box1.prop('checked')) {
urls.push(box1.value);
//Redirect by box1
}
EDIT4:完整代码
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="no-j-boxes.js"></script>
</head>
<body>
<input type="checkbox" name="box1" value="http://box1url.com" id="box1"><label>box1</label><br/>
<input type="checkbox" name="box2" value="http://box2url.com" id="box2"><label>box2</label><br/>
<input type="checkbox" name="box3" value="http://box3url.com" id="box3"><label>box3</label><br/>
<input type="checkbox" name="box4" value="http://box4url.com" id="box4"><label>box4</label><br/>
<input type="button" value="Submit" name="submitButton" onclick="submit()">
</body>
</html>
JavaScript:
function submit() {
var box1 = document.getElementById('box1');
var box2 = document.getElementById('box2');
var box3 = document.getElementById('box3');
var box4 = document.getElementById('box4');
var urls = [];
if (box1.checked) {
urls.push(box1.value);
}
if (box2.checked) {
urls.push(box2.value);
}
if (box3.checked) {
urls.push(box3.value);
}
if (box4.checked) {
urls.push(box4.value);
}
urls.forEach(function(url) {
goToUrl(url);
});
}
function goToUrl(url) {
var win = window.open(url, '_blank');
}