【问题标题】:Unable to get a javascript/html searchbar to work无法使 javascript/html 搜索栏工作
【发布时间】:2023-03-15 01:27:01
【问题描述】:

我无法让我的搜索栏返回结果,我正在尝试让搜索栏接受关键字,提交后它应该搜索我网站中所有 <div> 中的关键字,而不是单个页面和在我的 searchresults.html 页面中显示匹配项。

<script type="text/javascript">
    document.getElementById('button-submit').onsubmit = function() {
        window.location = 'http://www.google.com/search?q=site:webflicks.co ' + document.getElementById('button-submit').value;
        return false;
    }
</script>
<div class="parent">
    <div class = "search">
        <form id="searchbox" method="get" action="/search" autocomplete="off">
            <input name="q" type="text" size="15" placeholder="Enter keywords here..." />
            <input id="button-submit" type="submit" value=""/>	
        </form>
    </div>
</div>
</div>

【问题讨论】:

标签: javascript html search-engine


【解决方案1】:

您需要将您的提交事件处理程序绑定到表单,它不会在按钮上触发。

您还需要获取文本框的值,而不是按钮。

【讨论】:

    【解决方案2】:

    替换:

    <script type="text/javascript">
        document.getElementById('button-submit').onsubmit = function() {
            window.location = 'http://www.google.com/search?q=site:webflicks.co ' + document.getElementById('button-submit').value;
            return false;
        }
    </script>
    

    与:

    <script type="text/javascript">
       document.getElementById('button-submit').onsubmit = function() {
            window.location = "https://www.google.co.in/search?as_q="+ document.getElementById('button-submit').value +"&as_sitesearch=webflicks.co";            return false;
       }
        </script>
    

    希望这会对你有所帮助..!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多