Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript为google自定义搜索添加价值_Javascript_Jquery_Google Custom Search - Fatal编程技术网

如何使用javascript为google自定义搜索添加价值

如何使用javascript为google自定义搜索添加价值,javascript,jquery,google-custom-search,Javascript,Jquery,Google Custom Search,我想将从数据库获取的json数据添加到google自定义搜索javascript文件中,但我做不到,因为在google自定义搜索中,向输入框添加值的jquery方法失败了 需要帮忙吗 代码如下: (function() { var cx = '005172588709921972888:fsuibenly4m'; var gcse = document.createElement('script'); gcse.type = 'text/javascript';

我想将从数据库获取的json数据添加到google自定义搜索javascript文件中,但我做不到,因为在google自定义搜索中,向输入框添加值的jquery方法失败了

需要帮忙吗

代码如下:

(function() {
    var cx = '005172588709921972888:fsuibenly4m';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//cse.google.com/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
//checks if the id is empty
     if ( $('#gsc-i-id1').is(':empty')  ) {
       console.log('search is empty');
//if is empty add this code
       $(".gsc-input").val("Dolly Duck");
     }
     else {

       console.log('search is not empty');
   };
  })();
//the above code fails because i kept on seeing "search is not empty" in log
// i tried this one and it fails again
  $("#gsc-i-id1").val("Dolly Duck");
`


请提供任何帮助。

在查看了cse.google.com页面的谷歌开发者参考页面后,我能够解决这个问题。 我使用?q=lady+gaga来查询特定搜索的url,使用ajax的效果非常神奇


我在cse.google.com上查看了谷歌的开发者参考页面后,解决了这个问题。 我使用?q=lady+gaga来查询特定搜索的url,使用ajax的效果非常神奇

成功了