0

如何根据用户输入将浏览器定向到另一个 URL,例如:

abc.com/apple.html

abc.com/banana.html

abc.com/pear.html

但是,如果用户没有输入苹果、香蕉或梨,那么他们将被定向到:

abc.com/wrong.html

任何帮助都是极好的!我只知道 HTML 表单。

4

4 回答 4

0

你可以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-05T16:09:53.013 回答
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>

我在这里找到了上述方法:

W3Schools Window.Replace()

于 2013-01-05T16:12:04.367 回答
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>
于 2013-01-05T16:26:25.360 回答
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/,您只需添加、更改页面列表;)

于 2013-01-05T16:34:26.007 回答