javascript - 通过点击和键盘输入实现功能

标签 javascript twitter-bootstrap-3 bootstrap-modal

我想通过单击并按 Enter 键在对话框中执行 Javascript 函数。第一个有效。不幸的是,我无法正确实现保存按钮的功能(“addTask()”)。

<div class="modal fade" id="add_new_task_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-new-item" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Add new item</h4>
                </div>
                <div class="modal-body">

                    <ul class="alert alert-danger" ng-if="errors.length > 0">
                        <li ng-repeat="error in errors">
                            {{ error }}
                        </li>
                    </ul>

                    <div class="form-group">
                        <label for="quantity">Quantity</label>
                        <input ng-model="task.quantity" type="text" id="quantity" class="form-control"/>
                    </div>

                    <div class="form-group">
                        <label for="item">Item</label>
                        <input ng-model="task.item" type="text" id="item" class="form-control autocomplete"/>
                    </div>                   

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-primary" ng-click="addTask()">Save</button>
                </div>
            </div>
        </div>
    </div>

Javascript

$(function(){
        $('.modal-new-item').keypress(function(e){
          if(e.which == 13) {
            addTask();
          }
        })
      })

最佳答案

指令:


    app.directive('commonKeyEnter', function() {
        return {
            restrict: 'A',
            scope: {
                commonKeyEnter: '&'
            },
            link: function(scope, element, attrs) {
                element.bind('keyup keypress', function(event) {
                    if (event.which === 13) {
                        scope.$apply(function() {
                            scope.$eval(attrs.commonKeyEnter);
                        });

                        event.preventDefault();
                    }
                });
            }
        };
   });

HTML:


    <div class="modal-dialog modal-new-item" common-key-enter="addTask()" role="document">

我不确定“keyup”和“keypress”是否适用于 div,您可以尝试使用表单中的所有输入元素:



    <input ng-model="task.item" common-key-enter="addTask()" type="text" id="item" class="form-control autocomplete"/>

关于javascript - 通过点击和键盘输入实现功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51258838/

相关文章:

javascript - 从路径字符串优化嵌套数组

twitter-bootstrap - 关闭 Bootstrap Modal 不会停止 JW Player

jquery - 使用 Bootstrap 在图标旁边显示弹出窗口

javascript - 验证按钮单击的时间,以便在日期时间选择器中显示当天的剩余时间

javascript - 除非开发工具打开,否则 Bootstrap 模式不会在 IE11 中显示

javascript - 谷歌地图 :Knowing what marker the event was fired for

javascript - Firebase 节点删除后 View 没有变化

javascript - 同一页面中API对不同数据的不同请求,React(无Redux)

angularjs - 如何让 UI Bootstrap 选项卡在选项卡更改时发送事件

html - Bootstrap : Content in middle of the page