[英]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.