在javascript中调用自定义谷歌搜索标签时出现问题

Issue while calling custom google search tag in javascript

本文关键字:标签 问题 搜索 谷歌 javascript 调用 自定义      更新时间:2023-09-26

我使用的是谷歌自定义搜索api,但我不想要谷歌搜索默认文本框,所以出于同样的原因,我添加了一个新的文本框和一个按钮。点击我的自定义按钮后,我会从我的自定义文本框中获取搜索关键字值,并将其放入谷歌搜索默认文本框中。但在我的javascript函数中,我无法执行谷歌搜索提交按钮的点击事件。我现在知道谷歌搜索提交按钮的类名了,我该如何在我的java脚本函数中完成点击事件?

谷歌搜索dubmit按钮类名为"gsc搜索按钮gsc-search-button-v2"

现在,在下面的方法中,我需要执行上面类的click事件。

<script>
function myFunction() {
   document.getElementById('gsc-i-id1').value = document.getElementById('customTextBox').value;
 // here I need to perform click event for "gsc-search-button gsc-search-button-v2" class
}
</script>script>

我在下面的链接中有一个演示示例,

http://jsfiddle.net/3L4fd63g/3/

任何建议请

我看到了jquery标记,因此有一个选项用于提交表单,因此文本框上的回车键也将执行搜索。

此外,要执行单击,您只需要调用.click()

// Make sure in jsFiddle you have selected option onLoad.
(function() {
  var cx = '017643444788069204610:4gvhea_mvga'; // Insert your own Custom Search Engine ID here
  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);
})();
$(function(){
  $('#search').submit(function(e){
    $('.gsc-input').val($('#customTextBox').val());
    $('input.gsc-search-button').click();
    e.preventDefault();
  });
});
.search-box{
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="search">
  <input type="text" id="customTextBox"/>
  <input type="submit" value="Click me" />
</form>
<div class="search-box">
    <gcse:searchbox></gcse:searchbox>
</div>
<gcse:searchresults></gcse:searchresults>