使用 Jquery 提交 Google 自定义搜索



我使用谷歌自定义搜索,我想使用我自己的div和Jquery提交它的表单。我尝试了很多没有运气的事情,这是其中之一:

谷歌代码:

<script>
  (function() {
    var cx = 'partner-pub-2789521296837340:9402765321';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>

<gcse:search></gcse:search>

Jquery 部分:

$( document ).ready(function() {
$( "div.button" ).click(function() {
  $( "form.gsc-search-box" ).submit();
    return false;
});

});

活生生的例子:JsFiddle

我们不能直接提交表单(即使用jQuery在内部创建的Google)。您必须更改代码才能更改外观。这是您正在寻找的那个:

<form id="cse-search-box" action="http://google.com/cse">
  <input type="hidden" name="cx" value="YOUR SEARCH ENGINE ID goes here" />
  <input type="hidden" name="ie" value="UTF-8" />
  <input type="text" name="q" size="31" />
  <input type="submit" name="sa" value="Search" />
</form>
<img src="http://www.google.com/cse/images/google_custom_search_smwide.gif">

参考: https://support.google.com/customsearch/answer/1351747?hl=en

演示小提琴

最新更新