【问题标题】:How to open select box when pressing tab key?按tab键时如何打开选择框?
【发布时间】:2015-07-04 15:53:45
【问题描述】:

当按下 tab 键时,焦点会切换到另一个字段。我想要的是,当焦点到达<select> 时,会显示选项列表。

这是有效的:

  <div ng-controller="TypeaheadCtrl">
  <input type="text" /><br>
    <input type="text" /><br>
    <input type="text" ng-model="selected" typeahead="state for state in states " />

</div>

这不起作用:

<div ng-controller="TypeaheadCtrl">
  <input type="text" /><br>
    <input type="text" /><br>
    <!-- this is not working -->
    <select  ng-model="selected" >
                            <option value="aa">aa</option>
                            <option value="bb">bb</option>
                        </select>
</div>

我的插件代码是here

【问题讨论】:

标签: javascript html angularjs select angularjs-directive


【解决方案1】:

你可以使用这个指令,它会在焦点上打开选择框

指令

.directive('openSelect', function(){
  return{
    restrict: 'AE',
    link: function(scope, element, attrs){
      element.on('focus', function(){
        element.prop('size',element[0].options.length);
      })
      element.on('blur change', function(){
        element.prop('size',1);
      })
    }
  }
})

标记

<select ng-model="selected" open-select>
  <option value="aa">aa</option>
  <option value="bb">bb</option>
</select>

Working Plunkr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-06
    • 2015-01-24
    • 1970-01-01
    • 2020-10-24
    • 2021-04-02
    • 2014-08-19
    • 2015-07-17
    • 2012-12-28
    相关资源
    最近更新 更多