使用javascript验证<select>元素

时间:2015-09-24 06:31:14

标签: javascript

如何检查下面的下拉列表中没有值? &LT; TD&GT;     选择角色 &LT; / TD&GT; &LT; TD&GT;     &lt; select name =“RArole”id =“RArole”required&gt;         &lt; option value =“”&gt;选择&lt; / option&gt;         &lt; option value =“admin”&gt; Admin&lt; / option&gt;     &LT; /选择&GT; &LT; / TD&GT;

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
function getValue() {
  var e = document.getElementById("RArole");
  var val = e.options[e.selectedIndex].value;
  alert(val.length);
}
&#13;
<select name="RArole" id="RArole" required onchange="getValue()">
  <option value="">Select</option>
  <option value="admin">Admin</option>
</select>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<script data-require="jquery@*" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script>

<script>
$(function(){
$("#RArole").on("change",function(){
var option = $("select").val();
if(option  === ""){
alert("No value");
}
});
});
</script>
<body>
<select name="RArole" id="RArole" required >
  <option value="">Select</option>
  <option value="admin">Admin</option>
</select>
</body>
</html>