开发者

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

开发者 https://www.devze.com 2023-02-14 17:25 出处:网络
How can I check whether a option already exist in select by Prototype? I want to dynamically add options into select and so I need to check whether the opt开发者_StackOverflow中文版ion is already exi

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

I want to dynamically add options into select and so I need to check whether the opt开发者_StackOverflow中文版ion is already exist to prevent duplication.

I already See the same question with Jquery, but i try and try without success to do it with Prototype.

Thanks for any help.


try this

var exists = false;
$$('select#some-select-id option').each(function(el){
   if(el.value == val_to_check)
   {
      exists = true;
      throw $break;
   }
});

if(!exists) //add option


An alternate, less verbose alternative would be to use Prototype's find() method:

var exists = !!$$('select#some-select-id option').find(function(el){ return el.value == val_to_check });
0

精彩评论

暂无评论...
验证码 换一张
取 消