How can I check whether a option already exist in select by Prototype
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 });
精彩评论