我正在尝试使用Google使网站上的Search
功能正常工作。以下是HTML:
<!-- Start Search -->
<div class="top-search">
<div class="container">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-search"></i></span>
<input id="search" type="text" class="form-control" placeholder="Search">
<span class="input-group-addon close-search"><i class="fa fa-times"></i></span>
</div>
</div>
</div>
<!-- End Search -->
JS:
<!-- Search JavaScript -->
<script type="text/javascript">
document.getElementById('form-control').onsubmit = function() {
window.location = 'http://www.google.com/search?q=site:mywebsite.co.uk ' + document.getElementById('search').value;
return false;
}
</script>
<!-- End Search Javascript -->
最佳答案
您需要一个<form>
元素才能触发onsubmit
事件。将<input>
元素包装成表格,它将起作用。
<form>
<!-- Start Search -->
<div class="top-search">
<div class="container">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-search"></i></span>
<input id="search" type="text" class="form-control" placeholder="Search">
<span class="input-group-addon close-search"><i class="fa fa-times"></i></span>
</div>
</div>
</div>
<!-- End Search -->
</form>