0

たとえば、ユーザー入力に基づいてブラウザを別の URL にリダイレクトするにはどうすればよいですか。

abc.com/apple.html

abc.com/banana.html

abc.com/pear.html

ただし、ユーザーがapple、banana、またはpearを入力しない場合、次のように誘導されます。

abc.com/wrong.html

どんな助けでも素晴らしいでしょう!私は HTML フォームしか知りません。

4

4 に答える 4

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 に答える
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=します。

アップデート:

次の理由により、http メソッドは W3C によって推奨されていません。

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 に答える