简体   繁体   English

如何在javascript中做到这一点?

[英]how to do this effect in javascript?

the effect map is http://phplist.xxmn.com/1.jpg 效果图是http://phplist.xxmn.com/1.jpg

There are two searches engines. 有两个搜索引擎。 one is the site's default search. 一种是网站的默认搜索。 another is the google custom search. 另一个是Google自定义搜索。 When the vistor choses the site's default search, it should use the site's default search. 访问者选择站点的默认搜索时,应使用站点的默认搜索。 When they chose the google custom search option, it should use the google custom search. 当他们选择Google自定义搜索选项时,它应该使用Google自定义搜索。

the html code : 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"  /> 

This is the site's default search. 这是该网站的默认搜索。 I have added a select dropdown option to it. 我已经添加了一个选择下拉选项。 But I don't know how to use both. 但是我不知道如何同时使用两者。 When i chose the google search, it should use google search and when I choose the default search it should use the default search. 当我选择谷歌搜索时,它应该使用谷歌搜索,当我选择默认搜索时,它应该使用默认搜索。

I think you need to have the select inside the form first. 我认为您首先需要在form进行select Then, depending on the value of search_1 , you could redirect to http://www.google.com/?q=[the value of search_theme_form] . 然后,根据search_1的值,您可以重定向到http://www.google.com/?q=[the value of search_theme_form]

edit: if you are using jQuery, you could do something like this: 编辑:如果您使用的是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();
  }
});

note: I haven't tested the above code. 注意:我尚未测试以上代码。 Also, I would add / change your form element id attributes to something more meaningful, or at least put an id on the select. 另外,我会将您的表单元素ID属性添加/更改为更有意义的内容,或者至少将一个ID放在选择项上。

Of course, you could also implement this behaviour in your form handler code, which would probably be more sensible. 当然,您也可以在表单处理程序代码中实现此行为,这可能更明智。 If you're using PHP, something like this would work (assuming you haven't already sent page information): 如果您使用的是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
}

after class="search_l" in the select tag, add id="engine_dd" 在选择标记中的class="search_l"之后,添加id="engine_dd"

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

I think that's what you're asking for! 我认为这就是您要的! good luck :) 祝好运 :)

You can add an onchange event to the select tag, that changes the action attribute of the form depending on which option the user chooses. 您可以将onchange事件添加到select标记,该事件根据用户选择的选项来更改表单的action属性。 To make it simpler, get the value of the select tag on the server side and redirect to the proper search destination 为了使其更简单,请在服务器端获取select标记的值,然后重定向到正确的搜索目标

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

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