从单选按钮中选择选项后不会触发按键事件

keypress event does not triggered after choosing a choice from radio button

本文关键字:事件 单选按钮 选择 选项      更新时间:2023-09-26

使用backbone js我想在按 ENTER 键的同时执行一个函数。

我有一个表格:

  1. 当我在输入中输入 vallue 并按回车键时,该功能被触发并且工作正常。
  2. 当我选择一个按钮时,无线电并按回车键时,我的函数不会被调用。不行

我在我的视图中使用此代码:

View.FormCustomer = CommonViews.FormCustomer.extend({
    title : "Ajouter une fiche",            
},
events: {  
     "keypress": "getKeyCode"
},
getKeyCode: function(e){
    console.log(e.keyCode);
    if(e.keyCode == "13"){
        this.save(e);
    }               
},

有人有什么建议吗?

您可能必须在文档中而不是在事件对象中侦听该按键事件。

...
  initialize: function(){
  var _this = this;
    $(document).on('keypress.namespace', function(){
      _this.someMethod();
    }); 
    },
  ...
    //you'll have to remove that event when you ditch your view, assuming you have already override your remove method...
    remove: function(){
      $(document).off('.namespace');
    }