【问题标题】:How can I check whether a option already exist in select by Prototype如何通过原型检查选择中是否已存在选项
【发布时间】:2017-07-03 20:32:07
【问题描述】:

如何通过 Prototype 检查 select 中是否已存在选项?

我想动态地将选项添加到select中,所以我需要检查该选项是否已经存在以防止重复。

我已经看到与 Jquery 相同的问题,但我尝试使用 Prototype 进行尝试但没有成功。

感谢您的帮助。

【问题讨论】:

  • 我假设您推断的另一个问题是this one。看来需要做的就是用$$ 替换$。尝试时出了什么问题?

标签: prototypejs


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    另一种更简洁的替代方法是使用 Prototype 的 find() 方法:

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

    【讨论】:

      猜你喜欢
      • 2010-10-13
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 2013-03-17
      • 2017-04-04
      • 2012-04-07
      相关资源
      最近更新 更多