我正在使用基于本教程(http://code.tutsplus.com/tutorials/real-time-messaging-for-meteor-with-meteor-streams--net-33409)的Meteor构建的聊天应用程序,并且我试图将其设置在按住输入的位置,它提交您的消息而不必按“发送”按钮。
以下是应用用于通过按“发送”按钮提交评论的JavaScript代码,但有人知道如何添加输入功能吗?
// 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
});
});
答案 0 :(得分:2)
'keypress #send': function (evt) {
if (evt.which === 13) {
13是输入的密钥代码。如果你想改变它,你可以查找javascript keycodes,这样你就可以绑定你喜欢的任何东西。
您可能需要考虑熟悉api http://docs.meteor.com/
答案 1 :(得分:2)
您需要检查按键事件。您可以在此处找到每个密钥的完整代码列表:http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
执行此操作的最佳方法是使click事件中的函数成为命名函数,然后您可以在两个事件上运行相同的函数。
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
}
}
});