How can I check whether a option already exist in

2020-01-27 09:56发布

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 already exist to prevent duplication.

标签: jquery
7条回答
太酷不给撩
2楼-- · 2020-01-27 10:52

It's help me :

  var key = 'Hallo';

   if ( $("#chosen_b option[value='"+key+"']").length == 0 ){
   alert("option not exist!");

    $('#chosen_b').append("<option value='"+key+"'>"+key+"</option>");
    $('#chosen_b').val(key);
   $('#chosen_b').trigger("chosen:updated");
                         }
  });
查看更多
登录 后发表回答