如何根据用户输入将浏览器定向到其他URL,例如:
abc.com/apple.html
abc.com/banana.html
abc.com/pear.html
但是,如果用户没有输入苹果,香蕉或梨,那么他们会被引导到:
abc.com/wrong.html
任何帮助都会很棒!我只知道HTML表单。
答案 0 :(得分:1)
您可以使用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>
答案 1 :(得分:0)
在“abc.com”的HTML文件中,大部分是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>
我在这里找到了上述方法:
答案 2 :(得分:0)
<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>
答案 3 :(得分:0)
<script language="JavaScript">
var page = new Array("apple","banana","pear"); // list of your pages
function redirect(){
if(page.indexOf(document.forms["NameOfForm"]["NameOfInput"].value)!=-1){
window.location = document.forms["NameOfForm"]["NameOfInput"].value + ".html";
}
else {
window.location = "wrong.html";
}
return false;
}
</script>
<form name="NameOfForm" onsubmit="return redirect()">
<input name="NameOfInput" />
<input type="submit" />
</form>
将该代码放入abc.com/
,您只需添加,更改页面列表;)