套接字.IO聊天示例在表单提交后不起作用
socket.io chat example doesn't work after form submit
我遵循了socket的聊天教程。io (http://socket.io/get-started/chat/),但是一旦我来到了我必须从表格中获取信息的部分,我就卡住了。
我从index.html
文件中得到的警告是:
io is not defined please fix or add global io
$ is not defined please fix or add global $
我尝试将网站链接添加到<script src="/socket.io/socket.io.js">
,但这没有效果。我用c9。IO作为我的开发工具使用node.js和socket.io.
index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(process.env.PORT, process.env.IP, function() {
console.log("listening on *:" + process.env.PORT);
});
index . html
<!-- I did not include the html and head tags in this example for readability purposes -->
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
添加事件窗口就绪
var socket = io();
$(document).ready(function(){
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
相关文章:
- 加载后的页面与ajax表单提交不起作用
- 表单验证不起作用,并阻止表单提交
- 提交时AngularJS中的表单验证不起作用
- PHP+AAJAX表单提交不起作用
- Jquery、AJAX&PHP,表单提交不起作用
- 提交按钮在引导表单中不起作用
- Ajax表单提交不起作用
- jquery/ajax表单提交不起作用
- 重置按钮在表单提交之前有效,但在提交表单后不起作用
- Ajax 表单提交不起作用
- 提交时的表单验证不起作用
- Javascript 表单提交 prevent默认在使用 AJAX 检索表单时不起作用
- “返回假;”,因为表单提交不起作用
- 表单验证不起作用,用于在未经验证的情况下提交
- 当表单具有id为'的提交按钮时;提交'值,表单提交不起作用
- Jquery表单提交不起作用
- 通过中介函数使用超链接而不是按钮来提交表单是不起作用的
- 表单提交不起作用
- 我的Ajax表单提交不起作用
- 在jQueryAJAX中第二次尝试后表单提交不起作用