2016-11-25 84 views
2

沒有改變的價值,我有以下功能:在keydown事件

formCreated = function (event, data) { 
    console.log(data.formType); // output: 'edit' 

    $('body').on('keydown', function (e) {   
     if (e.ctrlKey && e.which == 80) { 
      e.preventDefault(); 
      e.stopPropagation(); 

      if (data.formType == 'create') // not changed until page refresh 
       alert('save the record'); 
      else if (data.formType == 'edit') 
       _connectPrinter(data); 
     } 
    }); 
} 

保存記錄data.formType變更爲「編輯」後,但該事件裏面的值不會改變,直到頁面刷新。

+0

您需要使用新值重新註冊它。由於之前的價值在封閉。 –

回答

2

嘗試http://api.jquery.com/off/

$('body').off('keydown').on('keydown', function (e) { 
    // your code here 
} 

的.off()方法刪除註冊值。

+0

謝謝@Deepart –