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

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

    var exists = $("#yourSelect option")
                   .filter(function (i, o) { return o.value === yourValue; })
                   .length > 0;
    

    This has the advantage of automatically escaping the value for you, which makes random quotes in the text much easier to deal with.

提交回复
热议问题