javascript HTML:如何创建搜索栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27580420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML: How to create search bar?
提问by Ban
I have this search bar:
我有这个搜索栏:
<div id="tfheader">
<form id="tfnewsearch" method="get" action="http://mywebsite.com">
<input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
</form>
<div class="tfclear"></div>
</div>
It worked great but when I search "keyword", it will redirect to http://mywebsite.com/?q=keyword.
它工作得很好,但是当我搜索“关键字”时,它会重定向到http://mywebsite.com/?q=keyword。
How to redirect to http://mywebsite.com/keyword? Thank you very much !
如何重定向到http://mywebsite.com/keyword?非常感谢你 !
回答by baao
You can do it with javascript
你可以用javascript来做
<script>
var a = document.getElementById('tfnewsearch');
a.addEventListener('submit',function(e) {
e.preventDefault();
var b = document.getElementById('tftextinput').value;
window.location.href = 'http://mywebsite.com/'+b;
});
</script>
and give your input field an ID called tftextinput.
并为您的输入字段提供一个名为 tftextinput 的 ID。
<input type="text" class="tftextinput" id="tftextinput" name="q" size="21" maxlength="120">
I don't really understand why you would like to do this, as it is way more difficult to handle this request server side as it would be to simply handle the $_GET data you will receive when doing a standard form transmission.
我真的不明白您为什么要这样做,因为处理这个请求服务器端要困难得多,因为它会简单地处理您在进行标准表单传输时将收到的 $_GET 数据。
EDIT:
编辑:
Here is the full code:
这是完整的代码:
<div id="tfheader">
<form id="tfnewsearch" method="get" action="http://www.mywebsite.com">
<input type="text" class="tftextinput" id="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
</form>
<div class="tfclear"></div>
</div>
<script>
var a = document.getElementById('tfnewsearch');
a.addEventListener('submit',function(e) {
e.preventDefault();
var b = document.getElementById('tftextinput').value;
window.location.href = 'http://mywebsite.com/'+b;
});
</script>