I';我很难理解这个javascript代码片段的意思。有人能逐行给出评论吗?

I';我很难理解这个javascript代码片段的意思。有人能逐行给出评论吗?,javascript,io-socket,Javascript,Io Socket,我知道它应该接收文本并将其发送到每个套接字,但我不确定每行的作用 <script src="/socket.io/socket.io.js"></script> <script src="https://code.jquery.com/jquery-1.11.1.js"></script> <script> $(function () { var socket = io(); $('form').submit(functio

我知道它应该接收文本并将其发送到每个套接字,但我不确定每行的作用

<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
  $(function () {
  var socket = io();
  $('form').submit(function(){
  socket.emit('chat message', $('#m').val());
  $('#m').val('');
  return false;
    });
  });

$(函数(){
var socket=io();
$('form')。提交(函数(){
emit('chat message',$('#m').val());
$('m').val('');
返回false;
});
});

我想说,这段代码有点不正确,因为它会在任何表单提交时触发-如果您有多个表单,您最终会遇到麻烦:)

//文档准备就绪时
//$(document).ready(function()的jQuery快捷方式{
$(函数(){
//创建一个新套接字并将其分配给socket变量
var socket=io();
//提交表格时
$('form')。提交(函数(){
//发出包含值的“聊天信息”事件
//具有'm`id的元素的
emit('chat message',$('#m').val());
//将该元素的值设置为空字符串
$('m').val('');
返回false;
});

})
也许从这里开始:我怎样才能指定哪个表单?你能举个例子吗?你可以用id属性标记表单,就像本例中的
m
一样,然后用
'yourid'
替换
'form'
。例如,如果你将提交的表单更改为id属性'chat'-like
然后您将使用
$(“#聊天”)
而不是
$(“#形式”)
$(function() {
  // create a new socket which connects to `/` by default
  var socket = io()
  // add a submission handler to any form on the current page
  $("form").submit(function() {
    // emit the value stored in the element identified by the id `m` as an argument for the `chat message` event in socket io
    socket.emit("chat message", $("#m").val())
    // clear the value stored in `#m`
    $("#m").val("")
    // prevent event propagation
    return false
  })
})