开发者

Add new items to a Html selectbox

开发者 https://www.devze.com 2022-12-17 19:52 出处:网络
Can I check for existing items in a html select box while adding them. A user types a text in a input box and then clicks button to add items. But I want to check for identical items before adding. Is

Can I check for existing items in a html select box while adding them. A user types a text in a input box and then clicks button to add items. But I want to check for identical items before adding. Is there a efficient way to write this 开发者_如何学JAVAscript?


Check if $('#dropdown option[value=' + newoption + ']') returns an option or not.

E.g.

if ($('#dropdown option[value=' + newoption + ']').length > 0) {
    // Option exist.
} else {
    // Option does not exist.
}


fix typo and shorten the logical check in js...

if ($("#dropdown option[value=' + newoption + ']").length) {
  // Option exist.
} else {
  // Option does not exist.
}
0

精彩评论

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