我正在使用谷歌自定义搜索:
1-我将文字“A”写入谷歌搜索框,
2-当我点击搜索按钮或点击回车时,javascript function_1/function_2
会将文本“B”(来自html文本框的文本,请参阅附带的代码示例)连接到文本“A” “,然后将对连接文本(”A“和”B“)触发搜索。
但是,看起来谷歌搜索处理器首先触发搜索只有文本“A”,然后连接完成..这不是我想要的。我希望对连接文本进行搜索。
请参阅附带的代码示例,如有任何解决方案,请告知我们。
<div class="gcse-container" id="gcse_container">
<gcse:search enableAutoComplete="true"></gcse:search>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
(function($, window) {
var elementName = '';
var initGCSEInputField = function_1() {
$( '.gcse-container form.gsc-search-box input.gsc-input' )
.on( "keyup", function( e ) {
if( e.which == 13 ) { // 13 = enter
var searchTerm = $.trim( this.value );
if( searchTerm != '' ) {
var elements = document.getElementById("firstname").value;
this.value += elements;
}
}
});
$( '.gcse-container form.gsc-search-box input.gsc-search-button' )
.on( "click", function_2( e ) {
var searchTerm = $.trim( $( '.gcse-container form.gsc-search-box input.gsc-input' ).val() );
if( searchTerm != '' ) {
var elements = document.getElementById("firstname").value;
searchTerm +=elements;
$( '.gcse-container form.gsc-search-box input.gsc-input').val(searchTerm);
}
});
};
var GCSERender = function() {
google.search.cse.element.render({
div: 'gcse_container',
tag: 'search'
});
initGCSEInputField();
};
var GCSECallBack = function() {
if (document.readyState == 'complete') {
GCSERender();
}
else {
google.setOnLoadCallback(function() {
GCSERender();
}, true );
}
};
window.__gcse = {
parsetags: 'explicit',
callback: GCSECallBack
};
})(jQuery, window);
(function() {
var cx = '017643444788069204610:4gvhea_mvga';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = 'https://www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>
答案 0 :(得分:1)
手动更改搜索字符串不是一个好主意,所以不要
$( '.gcse-container form.gsc-search-box input.gsc-input').val(searchTerm);
相反,您可以使用prefillQuery并执行google为此特定目的提供的api。尝试运行以下代码片段并搜索&#34; test&#34;,它会追加&#34; junit&#34;到查询字符串。
<div class="gcse-container" id="gcse_container">
<gcse:search enableAutoComplete="true"></gcse:search>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
(function($, window) {
var cseElement
var first_name = ' junit'
const initGCSEInputField = function() {
$( '.gcse-container form.gsc-search-box input.gsc-input' )
.on( "keyup", function( e ) {
});
$( '.gcse-container form.gsc-search-box input.gsc-search-button' )
.on( "click", function( e ) {
var searchTerm = $.trim( $( '.gcse-container form.gsc-search-box input.gsc-input' ).val() );
//use google api to prefill your query string and programmatically execute query
cseElement.prefillQuery(searchTerm + first_name)
cseElement.execute(searchTerm + first_name)
});
};
var GCSERender = function() {
cseElement = google.search.cse.element.render({
div: 'gcse_container',
tag: 'search'
});
initGCSEInputField();
};
var GCSECallBack = function() {
if (document.readyState == 'complete') {
GCSERender();
}
else {
google.setOnLoadCallback(function() {
GCSERender();
}, true );
}
};
window.__gcse = {
parsetags: 'explicit',
callback: GCSECallBack
};
})(jQuery, window);
(function() {
var cx = '017643444788069204610:4gvhea_mvga';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = 'https://www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>
&#13;