如何使用输入框重定向到同一 URL 上的特定端口

问题描述 投票:0回答:1

我试图有一个 html 输入框,在该框中我输入一个数字,然后提交重定向到该 URL 和端口。

就像我在 mysite.com 上,有一个输入框说我输入 1234,然后单击“提交”,它会重定向到 mysite.com:1234

希望这是有道理的。

是的,我确实尝试过这个

<form method="POST" action=":">
    <input type="number" name="" placeholder="Search.." />
    <input type="submit" value="Go" />
</form>

但是我得到了结果 mysite.com:/

但需要 mysite.com:1234 (或在框中输入的内容)

html forms redirect port
1个回答
0
投票
document.getElementById('myForm').addEventListener('submit', function(event) {
    event.preventDefault(); 

    // Get the value from the input field
    const port = document.getElementById('portNumber').value;
    const baseUrl = "..ht.tp://www.mysite.com" // u know what i'm trying to write, stackoverflow wont let me write http
    window.location.href = `${baseUrl}:${port}`;
    
});

假设您的表单具有 ID myForm,这应该可以工作。在你的 JS 脚本中使用它。 这里还没有检查 URL 是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.