我想提交一个谷歌自定义搜索查询而不重新加载/刷新整个 html 页面。我正在使用带有“仅结果”布局的最新 v2 gcs。
在搜索表单上方的任何位置加载 gcs api
<script src="//www.google.com/jsapi" type="text/javascript"></script>
<script>
google.load('search', '1',
{language : 'en', style : google.loader.themes.V2_DEFAULT});
</script>
我的自定义搜索表单
<form onsubmit="return executeQuery();" id="cse-search-box-form-id">
<input type="text" name="q" id="cse-search-input-box-id" size="25" autocomplete="off"/>
<input type="submit" id="site-search-submit" value="search"/>
</form>
gcs 结果脚本放置在需要搜索结果的任何位置
<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'en', style : google.loader.themes.V2_DEFAULT});
google.setOnLoadCallback(function() {
var customSearchOptions = {};
var customSearchControl = new google.search.CustomSearchControl(
'UNIQUE-API-KEY', customSearchOptions);
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
options.enableSearchResultsOnly();
customSearchControl.draw('cse', options);
function parseParamsFromUrl() {
var params = {};
var parts = window.location.search.substr(1).split('\x26');
for (var i = 0; i < parts.length; i++) {
var keyValuePair = parts[i].split('=');
var key = decodeURIComponent(keyValuePair[0]);
params[key] = keyValuePair[1] ?
decodeURIComponent(keyValuePair[1].replace(/\+/g, ' ')) :
keyValuePair[1];
}
return params;
}
var urlParams = parseParamsFromUrl();
var queryParamName = "q";
if (urlParams[queryParamName]) {
customSearchControl.execute(urlParams[queryParamName]);
}
}, true);
</script>
任何帮助表示赞赏。
谢谢
更新
我已经实施,
customSearchControl.execute(urlParams[queryParamName]);
现在我的搜索表单如下:
<form onsubmit="customSearchControl.execute(urlParams[queryParamName]);" id="cse-search-box-form-id">
<input type="text" name="q" id="cse-search-input-box-id" size="25" autocomplete="off"/>
<input type="submit" id="site-search-submit" value="search"/>
</form>
但是,执行搜索仍会刷新整个页面,这会在启动 jquery 脚本之前将我的初始 html 格式设置为混乱。
谢谢
更新
我已经以多种组合添加了以下所有变体,但要么刷新整个页面,要么根本没有任何反应。
<form onsubmit="return executeQuery(); return false;" id="cse-search-box-form-id">
<form onsubmit="executeQuery(); return false;" id="cse-search-box-form-id">
<form onsubmit="return false; executeQuery();" id="cse-search-box-form-id">
<form onsubmit="return false; return executeQuery();" id="cse-search-box-form-id">
<form onsubmit="customSearchControl.execute(urlParams[queryParamName]); return false;" id="cse-search-box-form-id">
<form onsubmit="return executeQuery(); event.preventDefault();" id="cse-search-box-form-id">
<form onsubmit="customSearchControl.execute(urlParams[queryParamName]); event.preventDefault();" id="cse-search-box-form-id">
<form onsubmit="customSearchControl.execute(urlParams[queryParamName]); event.stopPropagation();" id="cse-search-box-form-id">
等等...
有人有这方面的经验吗?用于进一步定制的 json api 呢?这会以某种方式解决页面刷新的问题吗?
谢谢