无法在节点js教程中提交表单

时间:2021-10-26 20:31:00

I am doing this http://socket.io/get-started/chat/

我这样做http://socket.io/get-started/chat/

I am on the section Emitting events

我正在发布事件部分

When I type a message in the text box and hit send it refreshes the page and takes me to the url localhost:3000/? instead of logging the message on the server.

当我在文本框中输入一条消息并点击发送时,它会刷新页面并将我带到网址localhost:3000 /?而不是在服务器上记录消息。

Why is the button failing to trigger the $('form').submit(function(){ line?

为什么按钮无法触发$('form')。submit(function(){line?

If I open the JS console on chrome and enter

如果我在chrome上打开JS控制台并输入

socket.emit("chat message", 'testing from console')

it logs the message in my commandline server window as expected. So I do not think there is an error in the server side code.

它按预期在我的命令行服务器窗口中记录消息。所以我不认为服务器端代码有错误。

I had hand typed out everything but when it wasn't working I copy pasted to make sure I had it right.

我有手打出所有东西,但是当它不工作时,我复制粘贴以确保我做对了。

So index.html for me is

所以index.html对我来说是

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
    <script src="https://cdn.socket.io/socket.io-1.2.0.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;
      });
    </script>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>

and index.js is

和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){
    console.log('message: ' + msg);
  });
});

http.listen(3000, function(){
    console.log('listneing on *:3000')
});

using chrome Version 47.0.2526.80 (64-bit)

使用chrome版本47.0.2526.80(64位)

1 个解决方案

#1


1  

Try wrapping your code in a document ready block. The form may not exist yet.

尝试将代码包装在文档就绪块中。表格可能尚不存在。

$(function(){
     $('form').submit(function(){
           socket.emit('chat message', $('#m').val());
           $('#m').val('');
           return false;
      });
 });

#1


1  

Try wrapping your code in a document ready block. The form may not exist yet.

尝试将代码包装在文档就绪块中。表格可能尚不存在。

$(function(){
     $('form').submit(function(){
           socket.emit('chat message', $('#m').val());
           $('#m').val('');
           return false;
      });
 });