2017-03-14 100 views
1

我試圖在用戶單擊按鈕和/或用戶按下Enter鍵時觸發一個函數。我不知道如何在同一個元素中存儲兩個事件。在HTML中的相同元素中使用多個事件

<td> <input type= "button" disabled id ="e2" value="Exercise 2" onclick ="loadQsets(2);setRadioValues(2);disableInput() ;" /></td> 

如何在同一個元素中使用onclick事件和enter鍵事件來觸發相同的函數?

+0

的關鍵碼您可以使用javascript –

回答

2

您需要處理​​事件,並把你的邏輯在那裏,if statement

見例如下。 13Enter

document.getElementById('inp').addEventListener('keydown', function(e){ 
 
    if(e.keyCode === 13){ 
 
    console.log('Enter is pressed !'); 
 
    } 
 
});
<input id="inp">

+0

的onkeypress事件謝謝。這有幫助 – MusicGirl

0

<td> <input type= "button" disabled id ="e2" value="Exercise 2" onclick ="loadQsets(2);setRadioValues(2);disableInput() ;" /></td>

window.onload=function(){ 
 
    var btn = document.getElementById('e2'); 
 
    
 
    function handleStuff() { 
 
    loadQsets(2); 
 
    setRadioValues(2); 
 
    disableInput(); 
 
    } 
 
    
 
    btn.onclick = function() { 
 
    handleStuff(); 
 
    }; 
 
    
 
    btn.onkeydown = function() { 
 
    handleStuff(); 
 
    } 
 
}
<td> <input type= "button" disabled id ="e2" value="Exercise 2" /></td>

相關問題