如何檢查下拉菜單中是否有任何選項作爲formvaldation?JS檢查下拉菜單中是否有選項
<div class="form-group has-warning-add has-feedback" id="div_add_eenheid">
<select class="form-control id="add_eenheid" name="add_eenheid" onclick="validate_add()" onmousemove="validate_add()">
<option value="">--- Selecteer eenheid ---</option>
<option value="stuk">stuk</option>
<option value="doos">doos</option>
<option value="kg">kg</option>
</select><span class="glyphicon glyphicon-warning-sign form-control-feedback" id="add_eenheid_status"></span>
</div>
function validate_add()
{
// eenheid
if(document.getElementById('add_eenheid').selected = true) { document.getElementById('div_add_eenheid').className = "form-group has-warning-add has-feedback"; document.getElementById('add_eenheid_status').className = "glyphicon glyphicon-warning-sign form-control-feedback"; }
else
{ document.getElementById('div_add_eenheid').className = "form-group has-success-add has-feedback"; document.getElementById('add_eenheid_status').className = "glyphicon glyphicon-ok form-control-feedback"; }
}
</script>
我曾嘗試:
getElementById('add_eenheid').selected = true
getElementById('add_eenheid').value == ''
getElementById('add_eenheid').selectedIndex == 0
任何幫助,非常感謝。
將'onclick =「validate_add()」'改爲onchange =「validate_add(this.value);」然後檢查該值是否不是空字符串。 – jeff
https://stackoverflow.com/questions/1271640/validate-select-box – sinisake
@jeff在document.getElementById('add_eenheid')。value ==''? – Muiter