在js中按下回车键时如何添加EventListener

时间:2021-06-19 15:55:30

标签: javascript addeventlistener keypress

我有 1 个 textbar,我想通过键盘上的 Enter 键 从用户那里获取值,然后运行一个函数(我的函数名称是 {{1} })。我的代码是这样的,对吗?

newNote

2 个答案:

答案 0 :(得分:-1)

我尝试找到正确的答案:

function nameOfYourEventListener(){
document.querySelector("#yourInputID").addEventListener("keydown", function (e){
if (e.code === 'Enter'){
    newNote(e); //function that you run for add the content to the list
}});

答案 1 :(得分:-2)

解决方案


function eventListeners(){

let targetInput = document.querySelector('#inputId');

document.querySelector("#note").addEventListener("keypress", function (e){
    if (e.key === "Enter"){
        
         let value = target.value; // do something with the value

        newNote();
    }
})

}