我不知道为什么这个剧本不起作用

时间:2013-11-03 17:46:58

标签: javascript node.js express socket.io pug

由于某种原因,此脚本无效。我正在使用express,socket.io,jade和node.js. 这是脚本:

var socket = io.connect();

function addMessage(msg) 
{
var currentDate = new Date();
var dateTime = currentDate.getDate() + "/" +
(currentDate.getMonth() + 1) + "/" + 
currentDate.getFullYear() + "@"
currentDate.getHours() + ":" +
currentDate.getMinutes() + ":" +
currentDate.getSeconds();
$("#historyView").append("<p>" + dateTime + " - " + msg + "</p>");
}

function sentMessage()
{
    if ($("#arduinoInput").val() != "")
    {
    socket.emit("message", $("#arduinoInput").val());
    addMessage($('#arduinoInput').val(), new Date().toISOString(), true);
    $("#arduinoInput").val("");
    }
  }

socket.on("message", function(message){
    addMessage(message);
});

$document.ready(function(){
$("#submit").click(function(){
    sentMessage();
});
});

它甚至没有清除文本框。这是玉页:

doctype 5
html
head
    title Arduino Controller 2
    script(src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js")
    script(src="/socket.io/socket.io.js");
    script(src="script.js")
    link(rel="stylesheet" type="text/css" href="style.css")
body
    div.container
        header
            h1 Arduino Controller 2
        center
            div#historyView
            input(type="text")#arduinoInput
            button#submit Send

我一直试图调试一段时间。我在Mac OS X 10.9上运行它,如果这有帮助的话。

1 个答案:

答案 0 :(得分:2)

假设您没有任何其他代码,这将是不正确的:

$document.ready();

您可能需要在文档中附加一个就绪处理程序,它应如下所示:

$(document).ready();