根据用户输入重定向

时间:2017-02-06 08:35:58

标签: javascript

我有20个不同的页面。假设页面的ID是例如:mysite.com/page1mysite.com/page2等等......

我需要创建一个输入字段,用户可以在其中输入数字,例如,如果输入数字16,则会将其重定向到mysite.com/page16

我相信这可以通过JavaScript实现。有没有人有任何建议?

3 个答案:

答案 0 :(得分:1)

window.onload = function() {
  document.getElementById('btn').onclick = function()
  {
    window.location.href = "http://www.yourdomain.com/page"+document.getElementById("url").value
  };
}
<input id="url" value="">
<button id="btn">Go!</button>

答案 1 :(得分:0)

您可以将window.redirect用于所需的页面

if (document.addEventListener) {
    window.addEventListener('pageshow', function (event) {
        if (event.persisted || window.performance && 
            window.performance.navigation.type == 2) 
        {
            location.reload();
        }
    },
   false);
}

其中var input_number = document.getElementById('input_id').value url = "mysite.com/page" + input_number; window.location=url; 是输入框的ID

答案 2 :(得分:0)

在vanilla JavaScript(无库)中,您可以使用以下脚本(ES6)。

&#13;
&#13;
let input = document.querySelector('#input');
input.addEventListener('change', event => {
  var url = `mysite.com/page${input.value}`;
  alert(url);
  window.location = url;
});
&#13;
<input id="input" type="number" name="page" min="1" max="16" value="0"></input>
&#13;
&#13;
&#13;