0

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

<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?kw=mysearchkeyword

如果 radio2 被选中?gw=mysearchkeyword

原因是对于“kw”,我使用的是我自己的个人搜索,而对于谷歌网络搜索,我需要“gw”参数来传递 url,请帮助

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

4

1 回答 1

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>
于 2012-04-06T18:10:06.603 回答