【问题标题】:How can I make this form redirect to example.com/i/username如何使此表单重定向到 example.com/i/username
【发布时间】:2017-08-15 21:41:09
【问题描述】:

这是我的html代码

<form method="get" action="https://example.com/i">
  Username : <input type="text" name="uname"></input>
  <br/>
  <input type="submit" value="search"></input>
</form>

当我在其中搜索某些内容时,它会重定向到 example.com/i/username/?,但我希望它重定向到 example.com/i/username。网址末尾不应有/和?

【问题讨论】:

  • 尝试使用 .../i/uname
  • 使用 JavaScript 进行重定向并通过 JavaScript 获取元素的值...我认为这种方式是这样做的方式
  • @Ashishsah 怎么样?
  • @Ashishsah 请不要将我与您不相关的 cmets 混淆。我期待答案。

标签: html forms redirect webforms


【解决方案1】:

这是要走的路……

var search = document.getElementById('search');
search.addEventListener('click', function() {
  var name = document.getElementById('name');

  //Redirect the client to the desired URL, if the name is valid.
  location.href = 'https://example.com/i/' + encodeURIComponent(name.value);
});
<form method="get" action="https://example.com/i" id="myForm">
    Username : <input type="text" id="name"></input><br/>
    <input type="button" value="search" id="search"></input>
</form>

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 2011-02-28
    • 2016-01-16
    • 2020-06-20
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多