【问题标题】:Use input javascript to redirect to input url使用输入 javascript 重定向到输入 url
【发布时间】:2021-01-07 00:50:35
【问题描述】:

所以你有一个搜索框并在那里输入一个单词/代码/东西,它会将你重定向到 example.com/the-input 我怎样才能做到这一点? 下面是我不工作的代码。

(我对 javascript 还很陌生,如果这听起来很愚蠢,我会尝试使用我在其他语言中的知识 sry ????)

<div class="search">
  <input type="text" class="searchTerm" id="link" placeholder="Write your code here...">
  <button onclick="search" type="submit" class="searchButton">
    <i class="fa fa-search"></i>
  </button>
</div>
<script language="JavaScript" type="text/javascript">
  function search() {
    var url = "http://www.example.com" + link;
    window.location = url;
  }
</script>

【问题讨论】:

  • 请说明您的问题 - 您的目标是什么,问题出在哪里,我们如何提供帮助?
  • 你有一个搜索框并在那里输入一个词/代码/东西,它会将你重定向到example.com/the-input
  • 右击搜索按钮时重定向?
  • 是的,点击按钮时重定向输入
  • 抱歉,新成员不清楚 - 编辑您的问题(现在不是问题)并添加此信息 - 您需要什么帮助?你有什么问题?

标签: javascript html redirect search input


【解决方案1】:

function search() {
  // The following line of code was split for readability
  // var url = "http://www.example.com/"+document.getElementById("link").value.split(" ").join("-");
  var url = "http://www.example.com/"+
      document.getElementById("link").value
      .split(" ").join("-");
  console.log(url);
  // window.location = url;
}
Search term(s) <input id="link" onchange="search()">
<button onclick="search()">Go to page+dash split search</button><br>
Will log to console for demonstration purpose.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多