谷歌自定义搜索与两个单选按钮

时间:2012-04-06 17:28:47

标签: javascript jquery

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

<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>

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

如果选中radio1

search.html?千瓦= mysearchkeyword

如果选中radio2?gw = mysearchkeyword

原因是对于“kw”我使用自己的个人搜索 对于谷歌网络搜索我需要“gw”参数来通过网址请帮助

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

1 个答案:

答案 0 :(得分:1)

一些问题:

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>