[英]How to display a text box while selecting an option from a multiselect dropdown
[英]Selecting from a dropdown box
我需要確保已選擇一個藝術家,但是不確定我應該使用“藝術家”還是“選擇”。 我都嘗試過,但沒有用。 如果沒有選擇,代碼必須顯示警報。
function validate() { var select_artist = document.getElementById('artist'); if (artist == "") { alert("You need to choose an artist"); return false; }
<div id="artist"> <p>Select an Artist: <select name="select"> <option value="0">Choose Artist</option> <option value="1">Bobby Smith</option> <option value="2">The Vibes</option> <option value="3">Kids of Rock</option> </select> </div>
您可以使用selectedIndex
;
function validate() {
var select_artist = document.getElementById('artistselect');
if (select_artist.selectedIndex < 1) {
alert("You need to choose an artist");
return false;
}
}
另外,您嘗試通過使用div元素來獲取選定的選項。 而是為select
元素設置id屬性。
<select name="select" id = "artistselect">
function validate() { var select_artist = document.getElementById('select'); if(select_artist.selectedIndex == 0) alert("You need to choose an artist") }
<div id="artist"> <p>Select an Artist: <select id="select" onchange="validate()"> <option>Choose Artist</option> <option >Bobby Smith</option> <option>The Vibes</option> <option>Kids of Rock</option> </select> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.