繁体   English   中英

如何在javascript中做到这一点?

[英]how to do this effect in javascript?

效果图是http://phplist.xxmn.com/1.jpg

有两个搜索引擎。 一种是网站的默认搜索。 另一个是Google自定义搜索。 访问者选择站点的默认搜索时,应使用站点的默认搜索。 当他们选择Google自定义搜索选项时,它应该使用Google自定义搜索。

html代码:

   <select class="search_l" >
        <option value="0">the default search</option>
         <option value="1">google search</option>
      </select>

 <form action="/"  accept-charset="UTF-8" method="post" id="search-theme-form"> 
 <div><div id="search" class="container-inline"> 
 <div class="form-item" id="edit-search-theme-form-1-wrapper"> 
 <input type="text" maxlength="128" name="search_theme_form" id="edit-search-theme-form-1" size="15" value="" title="put the search keyword in" class="form-text" /> 
</div> 
<input type="image" name="submit" id="edit-submit"  class="form-submit"    src="images/search_btn_top.gif" /> 
<input type="hidden" name="form_build_id" id="form-2" value="form-f02"  /> 
<input type="hidden" name="form_token" id="edit-search-theme-form-form-token" value="c5a"  /> 
 <input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form"  /> 

这是该网站的默认搜索。 我已经添加了一个选择下拉选项。 但是我不知道如何同时使用两者。 当我选择谷歌搜索时,它应该使用谷歌搜索,当我选择默认搜索时,它应该使用默认搜索。

我认为您首先需要在form进行select 然后,根据search_1的值,您可以重定向到http://www.google.com/?q=[the value of search_theme_form]

编辑:如果您使用的是jQuery,则可以执行以下操作:

jQuery ("#search-theme-form").submit (function () {
  if (jQuery (".search_1").val () == 1) {
    window.location.href = 'http://www.google.com/?q=' + 
      encodeURIComponent (jQuery ("#edit-search-theme-form-1").val ());
  }
  else {
    jQuery ("this").submit();
  }
});

注意:我尚未测试以上代码。 另外,我会将您的表单元素ID属性添加/更改为更有意义的内容,或者至少将一个ID放在选择项上。

当然,您也可以在表单处理程序代码中实现此行为,这可能更明智。 如果您使用的是PHP,则可以执行以下操作(假设您尚未发送页面信息):

//add 'name="search_target"' to your select element
if ($REQUEST['search_target'] == 1) { 
  header ("Location: http://www.google.com/?q=" . 
    urlencode ($REQUEST['search_theme_form']));
}
else {
  //process local search
}

在选择标记中的class="search_l"之后,添加id="engine_dd"

然后在id="search-theme-form"之后的表单标签中,添加onsubmit="function(){if(document.getElementById('engine_dd').selectedIndex == 1) window.location = document.getElementById('edit-search-theme-form-1').value;}"

我认为这就是您要的! 祝好运 :)

您可以将onchange事件添加到select标记,该事件根据用户选择的选项来更改表单的action属性。 为了使其更简单,请在服务器端获取select标记的值,然后重定向到正确的搜索目标

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM