在下拉列表中键入字符时,它正在从选项中选择任何随机匹配并触发更改事件

On typing character on drop down it is selecting any random match from options and triggering change event

本文关键字:事件 任何随 选项 字符 下拉列表 选择      更新时间:2023-09-26

下面是我在 backbonejs + coffeescript 中的代码。

场景:我打开国家下拉菜单,我输入"i"以选择以"i"开头的国家键入触发器更改下拉列表关闭的事件

预期:下拉菜单不应关闭,而应突出显示以"i"开头的国家/地区

我正在尝试获取仅在选择选项时触发的事件

注意:已经使用了jquery简单的下拉插件链接

Backbonejs 代码 -

class Brown.Views.Home.OperatorSignUp extends Backbone.View
  events:
    'change #country' : 'getState'
  getState: (event)->
    unless _.isEmpty($('#country').val())
      $('#state').html @statesTemplate(states: '')

网页代码 -

<div class="form-group colCountry customSelectRow">
  <select  id='country' name='country' class="form-control search">
    <option value="" class="countryLabel">Country</option>
    <% _.each(countries, function(val, key, object){%>
      <option value='<%=key%>'><%=val%></option>
    <% }) %>
  </select>
  <label class="floatingLabel">Country</label>
</div>

根据我在您提供的链接中找到的插件文档,您可以在任何选择元素上应用 easyDropdown 时为 onSelect 注册回调,如下所示

$(function(){
    var $selects = $('id-for-select');
    $selects.easyDropDown({
        onSelect: function(selected){
            // do something
        }
    });
});

所以我想你可以使用它来代替更改事件。