预填充Google自定义搜索字段

时间:2014-01-24 17:35:15

标签: jquery search-engine

我的网站上有一个Google自定义搜索引擎。我想要它,以便用户可以从2个选择菜单中选择值,搜索引擎文本字段将填充所选值。 Google提供给我的代码是:

<script>
  (function() {
    var cx = 'specialassignedcxvalue';
    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 on('change','#menu',function())调用会起作用,然后调用html()并传递值,但我不知道在哪里写值。有任何想法吗??提前谢谢。

2 个答案:

答案 0 :(得分:2)

经过更多研究后,无法预先填充Google搜索元素。它们只允许您通过最终用户的GET /search.php?q=term发送搜索查询。它似乎也反对他们目前的TOS

1.3你的义务。您将收到最终用户的查询,并将该查询转发给Google。

https://www.google.com/cse/docs/tos.html

答案 1 :(得分:0)

我有同样的问题,虽然我有一个真正的MESSY修复......

使用服务器端脚本(例如下面的PHP),您可以添加到&#34;空白/空白&#34;使用gsce属性webSearchQueryAddition =&#34;&#34;搜索结果。我很乐意,如果可能的话,有人可以通过Javascript / jQuery告诉我一个正确的方法。我想你可能需要有一个?q = *来欺骗页面进行搜索,这样你就可以将预先填好的查询添加到其中。

 <gcse:searchresults webSearchQueryAddition="<?php echo 'preFilledQuery'; ?>"></gcse:searchresults>

更多https://developers.google.com/custom-search/docs/element#supported_attributes

PS:我也尝试使用jQuery追加属性&#34; webSearchQueryAddition&#34;在加载之前使用<gcse>标记的值,但它似乎无法正常工作

相关问题