我有一个聊天应用程序,我希望用户只需按下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
    };
    });

推荐答案

您可以模拟点击按钮的过程.有.click()

将$(‘#idofextbox’)替换为CHAT_INPUT,然后使用Keycode或哪个属性来标识Enter键的按下:

chat_input.addEventListener('keypress', function (e) {
    var code = e.keyCode || e.which;
    if (code === 13) {
        // Enter key has been pressed
        chat_input_send.click(); // Trigger the send button's click event
        e.preventDefault(); // Prevent the default behavior of the Enter key (form submission)
    }
});

下面是修改后的代码:

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.addEventListener('keyup', function() {
    if (chat_input.value.length > 0) {
        chat_input_send.removeAttribute('disabled')
        chat_input_send.classList.add('enabled')
    } else {
        chat_input_send.classList.remove('enabled')
    }
});

chat_input.addEventListener('keypress', function (e) {
    var code = e.keyCode || e.which;
    if (code === 13) {
        chat_input_send.click();
        e.preventDefault();
    }
});

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();
};

此代码将使您能够通过在关注聊天输入字段时按Enter键来发送消息.它触发的逻辑与"Send"按钮的Click事件触发的逻辑相同.

希望它能有所帮助:D

Javascript相关问答推荐

为什么函数不使用以前函数返回的参数?

setFinder关闭模式并重定向到url

回归函数不会迭代到foreach中的下一个元素

在JavaScript中,如何将请求的回调函数的结果合并到运行的代码中?

在JavaScript中检索一些文本

有Angular 的material .未应用收件箱中的价值变化

node TS:JWT令牌签名以验证客户端和后台问题之间的身份验证

fs. writeFile()vs fs.writeFile()vs fs.appendFile()

如何解决chrome—extension代码中的错误,它会实时覆盖google—meet的面部图像?'

Angular 17—每当一个布尔变量变为真时触发循环轮询,只要它保持为真

未捕获错误:[]:getActivePinia()被调用,但没有活动Pinia.🍍""在调用app.use(pinia)之前,您是否try 使用store ?""

VUE 3捕获错误并呈现另一个组件

如何创建返回不带`new`关键字的实例的类

OpenAI转录API错误请求

在SHINY R中的嵌套模块中,不能使用Java代码

按什么顺序接收`storage`事件?

使用RxJS from Event和@ViewChild vs KeyUp事件和RxJS主题更改输入字段值

在SuperBase JS客户端中寻址JSON数据

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

是否设置以JavaScript为背景的画布元素?