提交的表单重定向到输入URL

Submitted form redirect to input URL

本文关键字:输入 URL 重定向 表单 提交      更新时间:2023-09-26

我正在尝试制作一个HTML表单,其中有一个标记为"输入您的商店ID"的文本框和一个submit按钮。

然后,当点击submit按钮时,我希望他们重定向到另一个网站,子域就是输入的内容。

示例:
如果输入"12345",它们将被重定向到12345.myothersite.com
如果输入"987654",它们将被重定向到987654.myothersite.com

既然您已经标记了这个jQuery,我想您可以接受jQuery的答案。

HTML:

<form id="form">
  <input id="subdomain"/>
  <input type="submit"/>
</form>

JavaScript:

$(document).ready(function() {
  $("#form").submit(function(event) {
    event.preventDefault();
    window.location.href = "http://" + $("#subdomain").val() + ".somesite.com/"
  });
});
window.location.href = document.getElementById('inputBox').value + '.myothersite.com';

示例:

https://jsfiddle.net/53q2jf2k/

<form onsubmit="return false">
    <input type="text" id="inputBox"/>
    <button  onclick="changeURL()" >Submit</button>
</form>

function changeURL() {
    window.location.href = document.getElementById('inputBox').value + '.myothersite.com';
  alert(document.getElementById('inputBox').value + '.myothersite.com')
}