如何将此表单重定向到example.com/i/username

时间:2017-08-15 21:41:10

标签: html forms redirect webforms

这是我的HTML代码



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

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

1 个答案:

答案 0 :(得分:0)

这里是要走的路......

&#13;
&#13;
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);
});
&#13;
<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>
&#13;
&#13;
&#13;