【问题标题】:How to redirect with parameters when clicked enter?单击输入时如何使用参数重定向?
【发布时间】:2023-01-01 11:59:00
【问题描述】:

我一直想知道如何使用 url 中的查询参数重定向到某个站点。

<input id="query" name="query" placeholder="Search" type="input" >

我试过使用 JavaScript 代码


<input id="query" name="query" placeholder="Search" type="input" onkeydown="if (event.keyCode == 13) submitForm()" >



<script>
  function submitForm() {
    var query = document.getElementById("query").value;
    window.location.href = "redirecturl?query=" + query;
  }
</script>

【问题讨论】:

    标签: javascript html forms redirect input


    【解决方案1】:

    采用

    window.location.replace( ... )
    

    代替

    window.location.href
    

    像:

    document.addEventListener('keydown', e => {
      if (e.key == "Enter") {
        const query = document.getElementById("query").value
        window.location.replace("https://google.com/search?q=" + query)
      }
    })
    &lt;input type="text" id="query"&gt;

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 2022-11-10
      • 2015-09-13
      • 2011-03-11
      • 2016-03-18
      • 2020-05-08
      • 2018-08-24
      • 2012-10-31
      • 1970-01-01
      相关资源
      最近更新 更多