Check if value is in select list with JQuery

前端 未结 7 2418
小蘑菇
小蘑菇 2020-12-13 17:06

How can I, using JQuery, check if a value belongs to dropdown list or not?

7条回答
  •  独厮守ぢ
    2020-12-13 17:23

    Use the Attribute Equals Selector

    var thevalue = 'foo';
    var exists = 0 != $('#select-box option[value='+thevalue+']').length;
    

    If the option's value was set via Javascript, that will not work. In this case we can do the following:

    var exists = false;
    $('#select-box option').each(function(){
        if (this.value == 'bar') {
            exists = true;
            return false;
        }
    });
    

提交回复
热议问题