如何添加回车键事件监听器?


btnSend.addEventListener('click', async(event)=>{
event.preventDefault()
let userInput = txtInput.value;
let temp = `<div class="out-msg">
<span class="my-msg">${userInput}</span>
<img src="static/img/me.jpeg" class="picture">
</div>`;
chatWindow.insertAdjacentHTML("beforeend", temp);
txtInput.value = ''
scrollSmoothlyToBottom()
txtInput.value = '';
const botResponse = await chatBotChat(userInput)
let tempBot = `<div class="out-msgbot">
<img src="static/img/bot.jpeg" class="picture">
<span class="my-msg">${botResponse}</span>
</div>`;
chatWindow.insertAdjacentHTML("beforeend", tempBot);
scrollSmoothlyToBottom()
})

这是从我的。html文件

<form class="popup-chat" action="">
<div class="title">Virtual Assitant</div>
<div class="badge">!</div>
<div class="chat-window" id="chat-window">
<div class="bot-msg">
<img src="{{ url_for('static', filename='img/bot.jpeg')}}" alt="" class="picture">
<span class="msg">Hi, How can i help you?</span>
</div>
<p id="scrollv"></p>
</div>
<div class="input-area" id="input-area">
<input autocomplete="off" type="text" name="msg" id="txtInput"/>
<button id="btn-emoji">&#127773;</button>
<button class="btn-send"> <i class="material-icons">send</i></button>
</div>
</form>

我应该如何添加一个输入键侦听器到这个代码?

我只能通过点击提交按钮发送消息。谢谢你的帮助。我是初学者。

希望对你有帮助。

document.onkeydown = (event) => {
if (event.key === "Enter") {
//Add your Enter event code here, like this.
alert('Hi, Insert what you want to run here');
}
}

最新更新