开发者

How to check if selected index of dropdownlist is not 0 or -1 using jquery

How to check if selected index of dropdownlist is not 0 or -1 开发者_如何学Gousing jquery?


Unless you've the actual option element to your hands, jQuery doesn't have special facilities for this. Just access the element's standard selectedIndex attribute:

var selectedIndex = $('#dropdownId').attr('selectedIndex');
if (selectedIndex > 0) {
    // ...
}


I had to check to see if selected index was greater than 0, so I used:

if($("select option:selected").index() > 0) {
  alert($("select option:selected").index());
}


$("select#elem").get(0).selectedIndex > 0


Taking a different approach from the others, jQuery can check the "value" of the option, as well.

<select id="checkme">
    <option value="0">0</option>
    <option value="-1">-1</option>
</select>

And the jQuery:

$(document).ready( function () {
    var theValue = $("#checkme").val();
    alert("The value of the select is: " + theValue);

});

This way you don't have to know what index maps to what value, you just check the value of the select, and it will tell you what option is selected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜