带有两个单选按钮的谷歌自定义搜索

google custom search with two radio buttons

本文关键字:单选按钮 谷歌 自定义 搜索 两个      更新时间:2023-09-26

这是我用于自定义搜索的代码,

<div id="cse-search-form" style="width: 100%;">Loading</div>
<script src="http://www.google.com.pk/jsapi" type="text/javascript"></script>
<script type="text/javascript"> 
google.load('search', '1', {language : 'en', style : google.loader.themes.V2_DEFAULT});
$('input[@name=sr]').change(function() {
 if ($("input[@name=sr]:checked").val() == 'kw')              
    var qval = "kw"
 else 
    var qval = "gw"
});
google.setOnLoadCallback(function() {
var customSearchOptions = {};  var customSearchControl = new google.search.CustomSearchControl(
  '009317721292264254327:ce_olwjr2z4', customSearchOptions);
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);  
var qval = $("input[@name=sr]:checked").val();
var options = new google.search.DrawOptions();
options.setAutoComplete(true);  
options.enableSearchboxOnly("http://www.alhudapk.com/audios/search.html", qval);
customSearchControl.draw('cse-search-form', options);
}, true); 
</script>
<div style="margin-top: 10px;">
<input type="radio" name="sr" id="kw" value="kw" checked="checked"  />Audio
<input type="radio" name="sr" id="gw" value="gw" / >Web
</div>
<style type="text/css">
input.gsc-input, .gsc-input-box, .gsc-input-box-hover, .gsc-input-box-focus {
border-color: #D9D9D9;
}
input.gsc-search-button, input.gsc-search-button:hover, input.gsc-search-button:focus {
border-color: #2F5BB7;
background-color: #357AE8;
background-image: none;
filter: none;
}
.gsc-search-box {
margin-top: -5px !important; 
margin-bottom: -5px !important;
}
</style>

我只需要一个简单的结果,即

如果选中单选 1

search.html?kw=mysearchkeyword

如果选中 radio2?GW=MySearchKeyword

原因是对于"kw",我使用的是自己的个人搜索对于谷歌网络搜索,我需要"GW"参数来传递 URL,请帮助

我的代码在页面加载后不起作用,它只工作一次,但在页面加载后不会更改任何值

一些问题:

1)语法错误:必须input[name$=sr] input[@name=sr]

2)局部变量qvar没有意义

3) 每次单选按钮更改时必须调用options.enableSearchboxOnly

试试这个:

        <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(
                   '009317721292264254327:ce_olwjr2z4', customSearchOptions);
               customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);  
               var options = new google.search.DrawOptions();
               options.enableSearchboxOnly("http://www.alhudapk.com/audios/search.html",
                                          $("input[name$=sr]:checked").val() );
               options.setAutoComplete(true);  
               customSearchControl.draw('cse-search-form', options);
               $('input[name$=sr]').change(function() {
                   options.enableSearchboxOnly("http://www.alhudapk.com/audios/search.html",
                     $("input[name$=sr]:checked").val() );
                   customSearchControl.draw('cse-search-form', options);
              });
            }, true); 
       </script>