2013-08-20 34 views
2

我試圖用一個「on」函數執行兩個事件。我有這樣的代碼:如何在jquery中添加多個事件委託並注意

<input type="text" id="lesson" /> 

$('#lesson').on("focusout keyup",function(e){ 
    var $change_education = $(this); 
    if(e.which === 188){ 
     var current_value = $(this).val(); 
     add_edit_tags(current_value, $change_education); 
    } 
}); 

該keyup事件正在工作,但重點不是。我無法從網絡上看到任何解決方法。

感謝

+0

什麼是'lesson'元素類型,你也在檢查'e.which',如果它是一個輸入類型的焦點輸出 –

+0

,它將不會被設置。看看我的編輯 – comebal

+0

@comebal重要的是因爲'focusout'沒有被所有元素觸發。 – alex

回答

1

的問題似乎是,你檢查e.which === 188如果它是一個focusout事件,將不會設置,如下改變條件和檢查

$('#lesson').on("focusout keyup",function(e){ 
    var $change_education = $(this); 
    if(e.type == 'focusout' || e.which === 188){ 
     var current_value = $change_education.val(); 
     add_edit_tags(current_value, $change_education); 
    } 
}); 

演示:Fiddle

+0

感謝您在我的代碼中指出錯誤。 – comebal