2017-03-14 62 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>

相关问题