[英]Submit message by pressing enter?
我正在基於本教程( 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
});
});
'keypress #send': function (evt) {
if (evt.which === 13) {
輸入的密碼為13。 如果要更改它,可以查找javascript鍵代碼,以便可以綁定到任何您喜歡的東西。
您可能需要考慮熟悉api http://docs.meteor.com/
您需要檢查按鍵事件。 您可以在此處找到每個鍵的代碼的完整列表: 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
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.