2013-11-24 48 views
-1

我有一個HTML查看頁面。我有一個下拉框,從那裏我可以選擇兩個值中的任何一個:OPEN和CLOSE。從這個下拉框中選擇一個值後,我需要點擊一個搜索按鈕。根據我從下拉框中選擇的值,該點擊將指引我到另一個視圖頁面。我怎樣才能做到這一點?????單擊按鈕時加載頁面,從下拉框中選擇一個值

這裏是我的下拉框和按鈕:

<td> 
        <select id="myChoice" class="combo"style="width: 110px; height: 25px"> 
         <option value="" class="combo">--Select--</option> 
         <option value="open">OPEN</option> 
         <option value="close">CLOSE</option> 
        </select></td> 
       <td> 
        <button type="button" style="width: 90px; height: 25px; font-size: 11px" onclick="$root.SearchCustomerProfile">Search</button> 
        </td> 

這裏是我的JavaScript函數:

self.SearchCustomerProfile = function() { 
    var customer_case = document.getElementById('myChoice').value; 

    if ((customer_case) == "open" || (customer_case) == "close") 
    { 
     window.location.href = '/CustomerProfileSearch'; 
    } 
} 
+0

請發佈你到底做了什麼樣的樣品 –

+0

然後把它縮小到一個簡單的例子,允許重現問題。併發布這個簡化的例子。 –

+0

好的,我編輯了我的文章,幷包含最相關的代碼片段。希望你能從中理解。請注意,點擊搜索按鈕後,我想加載CustomerProfileSearch頁面。 –

回答

0

我試圖創建一個小網站修復您的問題:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>get Dropdown Value</title> 
     <script> 
      window.onload = function(){document.getElementById("button").onclick= 
       function() { 
        var e = document.getElementById("select"), 
         txt = e.options[e.selectedIndex].text; 
       if(txt=="--Select--") 
        return; 
        alert("You clicked " + txt + ". You will be redirected to the " + txt + "-site."); 
        window.location = txt; 
       }; 
      }; 
     </script> 
    </head> 
    <body> 
     <select id="select"> 
     <option>--Select--</option> 
      <option>OPEN</option> 
      <option>CLOSE</option> 
     </select> 
     <button id="button">SEARCH</button> 
    </body> 
</html> 

您必須觸發button-onclick-event,然後訪問下拉列表的值。這可以通過document.getElementById("select").options[document.getElementById("select").selectedIndex].text;

在這個例子中,我給了dropdownlist id「select」來使用Javascript訪問它。

最後一件非常重要的事情是,您需要在創建onclick事件之前加載整個頁面。這就是爲什麼我使用「window.onload」

希望這有助於!

+0

我只需要一件事。我需要添加另一個選項:,這將是下拉列表中的默認選項,但選擇它將不會重定向。只有當用戶選擇OPEN或CLOSE時,纔會發生重定向。 –

+0

在這裏,我添加了一個if語句和你選擇選項。 – user2611844

相關問題