[英]Redirect based on user input
I have 20 different pages.我有 20 个不同的页面。 Let's say that IDs of the pages are for example:
mysite.com/page1
, mysite.com/page2
and so on...假设页面的 ID 是例如:
mysite.com/page1
, mysite.com/page2
等等...
I need to create an input field where the user can enter numbers, and if he inputs number 16 for example, it will redirect him to mysite.com/page16
.我需要创建一个输入字段,用户可以在其中输入数字,例如,如果他输入数字 16,它会将他重定向到
mysite.com/page16
。
I believe that this is possible with JavaScript.我相信这可以通过 JavaScript 实现。 Has anyone got any advice?
有没有人有任何建议?
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>
You could use window.redirect to your desired page您可以使用 window.redirect 到您想要的页面
var input_number = document.getElementById('input_id').value
url = "mysite.com/page" + input_number;
window.location=url;
Where input_id
is the id of your input box其中
input_id
是您输入框的 ID
In vanilla JavaScript (no libraries) you can use the following script (ES6).在 vanilla JavaScript(无库)中,您可以使用以下脚本 (ES6)。
let input = document.querySelector('#input'); input.addEventListener('change', event => { var url = `mysite.com/page${input.value}`; alert(url); window.location = url; });
<input id="input" type="number" name="page" min="1" max="16" value="0"></input>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.