如何根据用户输入将浏览器定向到另一个URL,例如:
abc.com/apple.html
abc.com/banana.html
Abc.com/ple.html
但是,如果用户没有输入apple、banana或pear,那么他们会被定向到:
Abc.com/bug.html
任何帮助都是很棒的!我只知道HTML表单。
发布于 2013-01-06 00:26:25
<form id='formName' name='formName' onsubmit='redirect();return false;'>
<input type='text' id='userInput' name='userInput' value=''>
<input type='submit' name='submit' value='Submit'>
</form>
<script type='text/javascript'>
function redirect() {
var input = document.getElementById('userInput').value;
switch(input) {
case 'apple':
window.location.replace('apple.html');
break;
case 'banana':
window.location.replace('banana.html');
break;
default:
window.location.replace('default.html');
break;
}
}
</script>发布于 2013-01-06 00:09:53
您可以使用Javascript/JQuery执行以下操作:
HTML:
<form name="form_input" action="post_values.php" method="post">
<input type="text" name="fruit" id="fruit" />
<input type="submit" value="Submit" />
</form>JS/JQuery:
<script>
$("form").submit(function() {
var fruit = $("#fruit").val();
if(fruit=='apple' || fruit=='pear' || fruit=='banana'){
window.location = "http://www.abc.com/"+fruit+".html";
}else{
window.location = "http://www.abc.com/wrong.html";
}
return true;
});
</script>发布于 2013-01-06 00:12:04
在"abc.com“的超文本标记语言文件(主要是index.html )中,在<HEAD>标记之间执行以下操作:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.abc.com/wrong.html">将content=调整为您希望浏览器在重定向之前等待的秒数。
更新:
由于以下原因,W3C不推荐使用http方法:
If a page redirects too quickly (less than 2-3 seconds), using the "Back" button on the next page may cause some browsers to move back to the redirecting page, whereupon the redirect will occur again. This is bad for usability, as this may cause a reader to be "stuck" on the last website.
因此,推荐使用JS的方法:
<head>
<script>
function replaceDoc()
{
window.location.replace("http://www.abc.com/wrong.html")
}
</script>
</head>我在这里找到了上面的方法:
W3Schools Window.Replace()
https://stackoverflow.com/questions/14173484
复制相似问题