这里是你的jQuery应该是什么样子的粗略轮廓:
$("form").submit(function(e) {
e.preventDefault(); // Prevents the page from refreshing
var $this = $(this); // `this` refers to the current form element
$.post(
$this.attr("action"), // Gets the URL to sent the post to
$this.serialize(), // Serializes form data in standard format
function(data) { /** code to handle response **/ },
"json" // The format the response should be in
);
});
这段代码发现页面上的所有表单元素和监听来自他们提交事件。表单可以通过多种方式提交(例如,点击提交按钮,敲入回车等),所以为了可用性,最好是直接监听提交事件,而不是在提交按钮上监听点击事件按键。
当发生提交事件时,上面的代码首先通过调用e.preventDefault
来防止默认浏览器操作(其中包括刷新页面)。然后它使用$.post将表单数据发送到action属性中指定的url。请注意,$.fn.serialize
用于以标准格式序列化表单数据。
您的明确的代码应该是这个样子:
var express = require('express')
, app = express.createServer();
app.use(express.bodyParser()); // Automatically parses form data
app.post('/Send', function(req, res){ // Specifies which URL to listen for
// req.body -- contains form data
});
app.listen(3000);
的文档上express.bodyParser
有点稀疏,但有点code spelunking后,它看起来像它使用node-querystring井盖下面。
希望这会有所帮助!
这是关闭主题,但考虑使用[标签标签](https://developer.mozilla.org/en/HTML/Element/label)包装“用户名:”和文本输入元素。它使表单更具语义和可用性。 – Xavi 2011-12-24 14:11:57