javascript - 创建一个提交表单,使用其输入来确定链接

标签 javascript html forms input

我想制作一个表单,提交后打开一个 html 文件,该文件使用同一选项卡中 URL 末尾的用户提交内容。

例如。提交=link1,打开code.html/link1(同一窗口)

到目前为止,在 window.open("code.html/"+ s... 后面加上 "_self"只会使网址末尾有一个问号,我被告知这是因为GET方法。

(Javascript)
<script>
var s = document.getElementById("submit").value;
function doFunction(){window.open("code.html/" + s);}
</script>

(HTML)
<form onsubmit="doFunction()">
  <input id="submit" type="text" />
</form>

最佳答案

也许你可以试试这个:

<form onSubmit="return doFunction();" target="_self">
 <input id="text-value" type="text" >
 <input type ="submit">
</form>

我还添加了提交按钮。

<script type="text/javascript">
   function doFunction(){
   var s = document.getElementById("text-value").value;
   var url = "code.html/" + s;
   var win = window.open(url, '_self');
   return false;
}
</script>

关于javascript - 创建一个提交表单,使用其输入来确定链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093095/

相关文章:

angular - 形式 Angular 6 的 ngModelChange

javascript - 如何随机化div的位置

html - 无法通过滚动为 div 中的最后一个元素提供边距?

javascript - 为除特定 ul 之外的所有无序列表分配动画

html - 制作具有双列的表格

c# - 播放MP3文件(C#表格)

javascript - 使用 jQuery 通过循环验证一系列单选按钮

javascript - Javascript Canvas 动画超出最大调用堆栈大小

javascript - 数据变量未从 VueJS 中的方法更新

javascript - Google 应用程序脚本不会记录提交