流星页面刷新与按钮单击
Meteor Page Refreshing with Button Click
我在这里遵循一些代码:http://code.tutsplus.com/tutorials/real-time-messaging-for-meteor-with-meteor-streams--net-33409
构建聊天应用。我想添加另一个文本输入框,该输入框也将项目发送到聊天框,但是每当我按下另一个按钮时,都会创建。换句话说,一个聊天框有两个输入框。
当我按下一个按钮时它可以工作,当我按下另一个按钮时,它会刷新页面并删除聊天框中之前的所有内容。任何帮助都会非常好。
<!-- Chat Box with chat messages and the input box -->
<template name='chatBox'>
<div id='messages'>
{{#each messages}}
{{>chatMessage}}
{{/each}}
</div>
<textarea id='chat-message'></textarea><br>
<button class='btn btn-primary' id='send'>Send Chat</button>
</template>
<!-- Template for the individual chat message -->
<template name='chatMessage'>
<div>
<b>{{user}}:</b> {{message}}
</div>
</template>
<template name="solBox">
<h3>Your problem is:</h3>
<strong>Write a structure definition in racket for a <em>vector</em> with fields <em>x</em> and <em>y</em>.</strong>
<br>
<br>
<form class="solution">
Solution:
<textarea id='solText'></textarea>
<button class='btn btn-primary' id='solve'>Send Solution</button>
</form>
</template>
//assign collection to the messages helper in chatBox template
Template.chatBox.helpers({
"messages": function(){
return chatCollection.find();
}
});
//generate a value for the 'user' helper in chatMEssage template
Template.chatMessage.helpers({
"user": function(){
if(this.userId == 'me'){
return this.userId;
} else if (this.userId){
getUsername(this.userId);
return Session.get('user-' + this.userId);
} else {
return 'anonymous-' + this.subscriptionId;
}
}
});
//When send chat is clicked add the typed chat message into the collection
Template.chatBox.events({
"click #send": function(){
var message = $('#chat-message').val();
console.log(message);
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
}
});
//Solutionbox stuff
Template.solBox.helpers({
"messages": function(){
return chatCollection.find();
}
});
Template.solBox.events({
"click #solve": function() {
var solution = $('#solText').val();
console.log(solution);
chatCollection.insert({
userId: 'me',
message: solution
});
$('#solText').val('');
solStream.emit('sol', solution);
}
});
chatStream.on('chat', function(message){
console.log(message + "on");
chatCollection.insert({
userId: this.userId, //get the userId of the sender
subscriptionId: this.subscriptionId, //subscription id of the sender
message: message
});
});
solStream.on('sol', function(solution){
console.log(solution + "on");
chatCollection.insert({
userId: this.userId, //get the userId of the sender
subscriptionId: this.subscriptionId, //subscription id of the sender
message: solution
});
});
我把它上传到网站:http://grcooper-wesolve-test.meteor.com/
当您单击表单内的按钮时,默认的浏览器行为是提交表单,这会导致另一个 HTTP 请求并重新加载页面。为了避免这种情况,事件处理程序需要显式阻止默认行为:
Template.solBox.events({
'click #solve': function(e) {
e.preventDefault();
// the rest of your code goes here
}
});
相关文章:
- 如何隐藏按钮单击事件上的占位符
- 从Web服务器下载图像按钮单击使用JavaScript
- 在SweetAlert中传递ASP.NET按钮单击事件
- 单选按钮单击可刷新/更改网站的小区域
- 调用按钮单击事件 ajax 加载的用户控件
- 附加 jQuery 代码以在加载和按钮单击时运行
- 在按钮单击时将图像URL数据显示到弹出框中,而无需禁用背景
- 按钮单击服务器单击
- 触发单选按钮单击,并在页面刷新时记住选择
- 如何使用 javascript 而不是使用控制器中的方法在 rails 中呈现部分按钮单击
- jQuery UI 选项卡 - 将参数设置为下一个/上一个按钮单击
- 如何在 Angular JS 中的按钮单击上添加类
- 当元素上有多个类时触发按钮单击事件
- 如何在 HTML 中的按钮单击上更改图像(IMG URL 每次来自服务器)
- 如何使用javascript刷新我的html页面时清除按钮单击
- 阻止后退按钮 - 单击按钮时忽略
- 如何制作按钮.单击“开始时不运行”
- Rails:尝试在按钮单击时渲染部分
- 在页面加载时调用函数,在按钮单击时再次调用函数,但参数不同
- 如何在服务器端的文本中添加按钮单击事件