我有一个聊天应用程序,我希望用户只需按下Enter键就可以发送消息. 目前,他们通过点击鼠标发送消息.
以下是我的代码:
var chat_input_send = document.createElement('button')
chat_input_send.setAttribute('id', 'chat_input_send')
chat_input_send.setAttribute('disabled', true)
chat_input_send.innerHTML = `<i class="far fa-paper-plane"></i>`
var chat_input = document.createElement('input')
chat_input.setAttribute('id', 'chat_input')
chat_input.setAttribute('maxlength', 1000)
chat_input.placeholder = `${parent.get_name()}. Say something...`
chat_input.onkeyup = function(){
if(chat_input.value.length > 0){
chat_input_send.removeAttribute('disabled')
chat_input_send.classList.add('enabled')
chat_input_send.onclick = function(){
chat_input_send.setAttribute('disabled', true)
chat_input_send.classList.remove('enabled')
if(chat_input.value.length <= 0){
return
}
parent.create_load('chat_content_container')
parent.send_message(chat_input.value)
chat_input.value = ''
chat_input.focus()
}
}else{
chat_input_send.classList.remove('enabled')
}
}
我知道这是一种方法,但我不能在我的代码中让它起作用:
有人能帮我吗?
$('#idoftextbox').keypress(function (e) {
var code = e.keyCode || e.which;
if (code === 13) {
//enter has been pressed
};
});