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

后端 未结 8 929
余生分开走
余生分开走 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:07

    I had a similar issue. Rather than run the search through the dom every time though the loop for the select control I saved the jquery select element in a variable and did this:

    function isValueInSelect($select, data_value){
        return $($select).children('option').map(function(index, opt){
            return opt.value;
        }).get().includes(data_value);
    }
    

提交回复
热议问题