Html 如何让我的搜索栏真正工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5207309/
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
How do i get my search bar to actually work?
提问by Sam
Hey guys, I've got a search bar and it looks fine, but i don't really know how to make it search the whole of my site... Here's my html code so far:
嘿伙计们,我有一个搜索栏,看起来不错,但我真的不知道如何让它搜索我的整个网站......这是我的 html 代码:
<form class="search2" method="get" action="default.html" />
<input class="search2" type="text" name="serach_bar" size="31" maxlength="255"
value="" style="left: 396px; top: 153px; width: 293px; height: 26px;" />
<input class="search1" type="submit" name="submition" value="Search" style=" padding-
bottom:20px; left: 691px; top: 153px; height: 23px" />
<input class="search2" type="hidden" name="sitesearch" value="default.html" />
Thanks in advance guys!
在此先感谢各位!
采纳答案by Infotekka
I know you said you want to skip the Google route, but in case you want an interim solution while you go down the path of writing your own code to search your site, this will help (I've expanded on what Brad Christie posted in his comment above):
我知道你说你想跳过谷歌路线,但如果你在编写自己的代码来搜索你的网站的过程中想要一个临时解决方案,这会有所帮助(我已经扩展了 Brad Christie 在他上面的评论):
Your HTML from above with element IDs added:
上面添加了元素 ID 的 HTML:
<form id="frmSearch" class="search2" method="get" action="default.html" />
<input class="search2" id="txtSearch" type="text" name="serach_bar" size="31" maxlength="255"
value="" style="left: 396px; top: 153px; width: 293px; height: 26px;" />
<input class="search1" type="submit" name="submition" value="Search" style=" padding-
bottom:20px; left: 691px; top: 153px; height: 23px" />
<input class="search2" type="hidden" name="sitesearch" value="default.html" />
The JavaScript:
JavaScript:
<script type="text/javascript">
document.getElementById('frmSearch').onsubmit = function() {
window.location = 'http://www.google.com/search?q=site:yoursitename.com ' + document.getElementById('txtSearch').value;
return false;
}
</script>
Since the JS to power the search is not inside a window.onload function you will need to place the script block afteryour form. I've tried to simplify this as much as possible to help you get it integrated and working right away. Oh, and don't forget to change "yoursitename.com" in the above JS to your actual domain name.
由于JS功率搜索是不是在window.onload函数里面,你会需要的脚本块放置后您的形式。我试图尽可能地简化它,以帮助您将其集成并立即工作。哦,别忘了把上面JS中的“yoursitename.com”改成你的实际域名。