How can I check whether a option already exist in select by JQuery

后端 未结 8 841
余生分开走
余生分开走 2020-11-30 19:22

How can I check whether a option already exist in select by JQuery?

I want to dynamically add options into select and so I need to check whether the option is alread

相关标签:
8条回答
  • 2020-11-30 20:09

    Although most of other answers worked for me I used .find():

    if ($("#yourSelect").find('option[value="value"]').length === 0){
        ...
    }
    
    0 讨论(0)
  • 2020-11-30 20:13

    Another way using jQuery:

    var exists = false; 
    $('#yourSelect  option').each(function(){
      if (this.value == yourValue) {
        exists = true;
      }
    });
    
    0 讨论(0)
提交回复
热议问题