2017-10-31 24 views
-4

テキストボックスから入力を受け取り、入力を使用してURLに誘導するJavaScriptを探しています。特定のスクリプトを作成するのに助けが必要

は、IDを入力します:_______

"123" を入力し、[送信]をクリックすると、それはwww.mywebsite.com/users/123.htmlのページへ移動しますを提出します。

+2

あなたはどのようなコードを試してみましたか?これはかなり簡単で、Googleを使用して見つけることができます。 – Matt

+0

誰かが推奨したスクリプトを試しましたが、うまくいきません。 それは番号を追加= 123はwww.mywebsite.com/users/123.htmlに行くのではなく、現在のURLに1233を入力してください。 –

+0

私はそれをオンラインで探しましたが、私が探しているものを見つけることができるならリンクをとるでしょう。 –

答えて

0
<html> 
    <body bgcolor="silver"> 
    <div class="align-center" > 
     <h2> Re-Direction</h2> 
    </div> 
    <label>Enter Your Search Interest</label> 
    <input type="text" id="data"/></br> 
    <!-- input type="text" would help you to create a box which would accept a user value --> 
    <div class="align-center"><button onclick="redirection()">Click Me</button></div> 
    <!-- in the above line we attach a JS function redirection to the button --> 
    </body> 
    <script> 
    function redirection(){ 
     //document.getElementById("data") would search for the html element with id="data" which in our case is input element 
     var tempData = document.getElementById("data"); 
     //window.location.href would help you with redirection to the desired url 
     window.location.href = 'https://en.wikipedia.org/wiki/'+tempData.value; 
     //window.location.href="your urli.e www.mywebsite.com/users/"+tempData.value; 
    } 
    </script> 
</html> 
+0

あなたは男です!ありがとう、相棒! –

+0

問題ありません。お力になれて、嬉しいです –

0

これを行う1つの方法は、フォームを作成し、送信する前にアクションを変更することです。

(function() { 
 
    var form = document.querySelector("form.js-form"); 
 
    if (form instanceof HTMLFormElement) { 
 
     var text = form.querySelector("input[type='text']"); 
 
     if (text instanceof HTMLInputElement) { 
 
      form.addEventListener("submit", function (evt) { 
 
       form.action = "www.mywebsite.com/users/" + text.value + ".html"; 
 
      }); 
 
     } 
 
    } 
 
})();
<form action="javascript:void(0)" method="GET" class="js-form"> 
 
    <input type="text" placeholder="Insert a user Id" /> 
 
    <input type="submit" value="submit" /> 
 
</form>
それとも、ただ入力してクリックしたときに、ウィンドウをリダイレクトするというボタンを持つことができます。

(function() { 
 
    var text = document.querySelector(".js-text"); 
 
    var submit = document.querySelector(".js-button"); 
 
    if (text instanceof HTMLInputElement && submit instanceof HTMLButtonElement) { 
 
     submit.addEventListener("click", function (evt) { 
 
      window.location.replace("www.mywebsite.com/users/" + text.value + ".html"); 
 
     }); 
 
    } 
 
})();
<input type="text" placeholder="Type an Id" class="js-text" /> 
 
<button class="js-button">Submit</button>

+0

ありがとうございますが、送信ボタンを押しても何もしません。 –

+0

スクリプトが実行中でエラーがないことを確認してください(赤いエラーのコンソールを確認してください) –

+0

いいえ 私はオフラインで作業しているので、 –

関連する問題