按Enter键提交消息? [英] Submit message by pressing enter?

查看:150
本文介绍了按Enter键提交消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于本教程的Meteor构建的聊天应用程序( http://code.tutsplus.com/tutorials/real-time-messaging-for-meteor-with-meteor-streams--net-33409 )我试图把它放在哪里,如果你按回车它提交你的消息而不必按发送按钮。

I am working on a chat app built with Meteor based off of this tutorial (http://code.tutsplus.com/tutorials/real-time-messaging-for-meteor-with-meteor-streams--net-33409) and I am trying to make it where if you press enter it submits your message instead of having to press the Send button.

下面是应用程序用来提交的javascript代码按发送按钮发表评论,但有人知道如何添加输入功能吗?

Below is the javascript code the app uses to submit a comment by pressing the Send button, but does anyone know how to add the enter feature?

// when Send Chat clicked add the message to the collection
Template.chatBox.events({
  "click #send": function() {
    var message = $('#chat-message').val();
    chatCollection.insert({
     userId: 'me',
     message: message
   });
   $('#chat-message').val('');

   //add the message to the stream
   chatStream.emit('chat', message);
  }
});
chatStream.on('chat', function(message) {
  chatCollection.insert({
    userId: this.userId,
    subscriptionId: this.subscriptionId,
    message: message
  });
});


推荐答案

您需要检查按键事件。您可以在此处找到每个密钥的完整代码列表: http ://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

You need to check for a key press event. You can find a full list of the codes for each key here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

最好的办法是在click事件中运行一个命名函数,然后你可以在两个事件上运行相同的函数。

Best way to do this would be make the function in the click event a named function and then you can just run the same function on both events.

Template.chatBox.events({
 "click #send": function() {
    var message = $('#chat-message').val();
    chatCollection.insert({
      userId: 'me',
      message: message
    });
    $('#chat-message').val('');

    //add the message to the stream
    chatStream.emit('chat', message);
  },
  "keypress #chat-message": function(e) { 
    if (e.which == 13) {
      console.log("you pressed enter");
      //repeat function from #send click event here
     }
  }
});

这篇关于按Enter键提交消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆