2012-06-09 56 views
0

我目前正在嘗試基於多個下拉菜單創建搜索。多個下拉搜索替換字符串

我使用的片段是:

<script type="text/javascript" charset="utf-8"> 
     function add_criteria (el) { 
      search = document.getElementById('search_criteria'); 
      search.value = document.getElementById('type').value + " " + document.getElementById('color').value;} 
    </script> 

    <form role="search" method="get" id="searchform" action="search.html?submit=Go&_nkw"> 
     <label class="screen-reader-text" for="s">Search for:</label> 
    Type 
    <select name='type' id='type' class='postform' onchange="add_criteria(this);"> 
     <option value='' selected='selected'>All</option> 
     <option class="level-0" value="dog">Dog</option> 
     <option class="level-0" value="cat">Cat</option> 
    </select> 

    Color 
    <select name='color' id='color' class='postform' onchange="add_criteria(this);"> 
     <option value='' selected='selected'>All</option> 
     <option class="level-0" value="red">red</option> 
     <option class="level-0" value="white">white</option> 
     <option class="level-0" value="green">green</option> 
    </select> 
     <input type="submit" id="searchsubmit" value="Search" /> 
     </div> 
    </form> 

結果卻是不正確的 - 什麼,我想實現的是顯示在這種方式的結果:

http://mystore.com/search.html?submit=Go&_nkw=medium+red 

有誰有一個想法,我可以如何讓這個更換刺固定和正常工作?

一些建議將高度讚賞 - 非常感謝你!

+1

'結果但不correct'。請向我們展示您的錯誤結果或生成錯誤的網址。你的意思是說編碼的URL不正確? – Ammar

+0

使用上面的代碼片段,搜索網址出現爲:search.html?type = dog&color = green但是我想實現它看起來像這樣:search.html?submit = Go&_nkw = dog + green –

回答

1

那麼,你只需要一個簡單的把戲。首先,讓那些<select>標記出<form>標籤,並做兩個假<input>與名submit_nkw(那<form>元素是如何工作的):

<!DOCTYPE html> 
<html> 
<body> 

<script type="text/javascript" charset="utf-8"> 
     function add_criteria (el) { 
      search = document.getElementById('search_criteria'); 
      search.value = document.getElementById('type').value + " " + document.getElementById('color').value;} 
    </script> 

     <div> 
     <select name='type' id='type' class='postform' onchange="add_criteria(this);"> 
      <option value='' selected='selected'>All</option> 
      <option class="level-0" value="dog">Dog</option> 
      <option class="level-0" value="cat">Cat</option> 
     </select> 

     Color 
     <select name='color' id='color' class='postform' onchange="add_criteria(this);"> 
      <option value='' selected='selected'>All</option> 
      <option class="level-0" value="red">red</option> 
      <option class="level-0" value="white">white</option> 
      <option class="level-0" value="green">green</option> 
     </select> 
     </br> 
     <form role="search" method="get" id="searchform" action="search.html"> 
      <input name="submit" value="Go" style="visibility:hidden;"></label> 
      <input id="search_criteria" name="_nkw" value="" style="visibility:hidden;"></label> 
      <input type="submit" id="searchsubmit" value="Search" /> 
     </form> 
     </div> 

</body> 
</html> 
+0

這是絕對優秀的 - 謝謝你非常感謝你的幫助!我希望你有一個愉快的週末 –