Check if value is in select list with JQuery

前端 未结 7 2422
小蘑菇
小蘑菇 2020-12-13 17:06

How can I, using JQuery, check if a value belongs to dropdown list or not?

7条回答
  •  一生所求
    2020-12-13 17:24

    I know this is kind of an old question by this one works better.

    if(!$('.dropdownName[data-dropdown="' + data["item"][i]["name"] + '"] option[value="'+data['item'][i]['id']+'"]')[0]){
      //Doesn't exist, so it isn't a repeat value being added. Go ahead and append.
      $('.dropdownName[data-dropdown="' + data["item"][i]["name"] + '"]').append(option);
    }
    

    As you can see in this example, I am searching by unique tags data-dropdown name and the value of the selected option. Of course you don't need these for these to work but I included them so that others could see you can search multi values, etc.

提交回复
热议问题