使用Backbone/Marionette在表单输入文本上按Tab键或输入时执行任务?

Uui*_*uid 3 javascript jquery backbone.js marionette

当有人完成在输入文本上添加/编辑数据并按下Backbone中的Enter或tab时,有没有办法可以执行我的方法?

Sco*_*leo 8

以下是在文本字段中返回时如何实现事件的示例:

var SearchView = Marionette.ItemView.extend({
    template: "#template",
    events: {
      'keypress #search-input' : 'searchKeywords',
    },
    searchKeywords: function(e){
      if ( e.which === 13 ) { 
        var keywords = $(e.target).val();

        if(keywords === '') return;

        this.model.set({keywords: keywords});
      }
    }
});    
Run Code Online (Sandbox Code Playgroud)

event.which属性规范化event.keyCode和event.charCode.

http://api.jquery.com/event.which/