簡體   English   中英

嘗試使用下拉列表的ID選擇在下拉列表中選擇的值

[英]Trying to select the value that was chosen in a drop down list using the ID of the drop down list

在此處輸入圖像描述,我正在嘗試檢索最終用戶在下拉列表中選擇的值。 可變國家/地區始終為null,我正在使用下拉列表的ID來檢索用戶選擇的值。

這是我認為的代碼:

    <script>
        var country = document.getElementById("Countries");
        var value = country.options[country.selectedIndex].value;
        var text = country.options[country.selectedIndex].text;
  </script>

@Html.Label("Country", "Country")
<select name="Countries" id="Countries">
    @foreach (var country in ViewBag.countries)
    {
        <option value="@(country.country)">@(country.country)</option>
    }
</select>

}

這樣嘗試

 <label for="Country">Country</label> <select name="Countries" id="Countries"> <option value="Iceland">Iceland</option> <option value="Kosovo">Kosovo</option> <option value="Belgium">Belgium</option> </select> 

var element = document.getElementById("Countries"); 
var selected_country_value = element.options[element.selectedIndex].value;    
alert(selected_country_value);

演示: https : //jsbin.com/poxijiluci/edit?html,js,輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM