Jquery 如何知道select元素没有项(选项数据)?

Jquery 如何知道select元素没有项(选项数据)?,jquery,Jquery,有一个select元素。在页面加载时,它没有数据选项。如何知道它没有数据选项?试试以下方法: if($('yourselector').length==0){ //nothing selected }else{ //there is something selected } 用这个来检查。。如果零,则不存在选项标记。检查子项的长度 $('select').children().length 如果返回0,则不会有子项 您可以获得如下选项的数量: var num = $('#your_selec

有一个select元素。在页面加载时,它没有数据选项。如何知道它没有数据选项?

试试以下方法:

if($('yourselector').length==0){
//nothing selected
}else{
//there is something selected
}

用这个来检查。。如果零,则不存在选项标记。

检查子项的长度

$('select').children().length
如果返回0,则不会有子项


您可以获得如下选项的数量:

var num = $('#your_select').children('option').length;
if(num < 1){
    // no option
}
演示:

希望这有帮助。

请尝试:jQueryyourSelector.is:empty,
Try this code.

<select id="opt">
<option></option>
</select>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var chckopt = document.getElementById("opt").value;
if(chckopt == "")
alert("empty");
});
</script>
Hope this is what you were asking for.
var num = $('#your_select').children('option').length;
if(num < 1){
    // no option
}
Try this code.

<select id="opt">
<option></option>
</select>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var chckopt = document.getElementById("opt").value;
if(chckopt == "")
alert("empty");
});
</script>
Hope this is what you were asking for.