Window.open()无法打开新网页?
是因为我的JavaScript,还是代码中的其他任何地方都存在问题?
function getage() {
var age = document.getElementById("customerage").value;
if (age > 21) {
window.open = "beer.com";
} else if (age > 0) {
window.open = "http://lol.disney.com/games/tsum-tsum-tower";
} else
alert("Please input correct age!");
}

<html>
<head>
<title>I drink alcohol.com</title>
<meta charset="utf-8">
<meta name="Sumei" content="Alcohol Web">
</head>
<body>
<h1>Welcom to "I drink alcohol.com"</h1>
<h3>Here is couple of questions for you:</h3>
<p>How old are you?</p>
<form method="post">
<input type="number" name="customerage" id="customerage" />
<input type="submit" onclick="getage()" value="OK">
</form>
<script>
/* getage function*/
</script>
</body>
</html>
&#13;
答案 0 :(得分:0)
如果我理解正确,你想根据迭代结果转到页面 试试这段代码,你也没有正确使用window.open(),应该像 window.open(URL,&#34; _blank&#34;,选项)一样使用; 如果你希望它在新标签页中打开
否则请转到下面的代码
if(age>21)
{
window.location="beer.com";
}
else if(age>0)
{window.location="http://lol.disney.com/games/tsum-tsum-tower";}
else
alert("Please input correct age!");
}
以及我认为你应该在你的其他地方添加一些东西,比如
else if(age>0 && age <21)
答案 1 :(得分:0)
只需使用window.open
var option= "location=yes,height=570,width=520,scrollbars=yes,status=yes";
var link = URL + location.href;
var win = window.open(link, "_blank", option);
答案 2 :(得分:0)
使用 window.open()在新标签页中打开,然后使用 window.location.href 在同一个标签页中打开链接
<html>
<head>
<title>I drink alcohol.com</title>
<meta charset="utf-8">
<meta name="Sumei" content="Alcohol Web">
</head>`
<body>
<h1>Welcom to "I drink alcohol.com"</h1>
<h3>Here is couple of questions for you:</h3>
<p>How old are you?</p>
<form method="post">
<input type="number" name="customerage" id="customerage"/>
<input type="submit" onclick="getage()" value="OK">
</form>
<script>
function getage() {
var age = document.getElementById("customerage").value;
if(age>21)
{window.open("beer.com");}
else if(age>0)
{window.open("https://www.google.com");}
else
alert("Please input correct age!");
}
</script>
</body>
</html>