node服务器获取form表单
搭建好服务器后
(前言,本文只是介绍form表单直接提供给 本页面数据,即在不刷新页面的前提下更改数据)
在public里面添加index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
</head>
<body>
<form method="post" action="hello">
<label>
name:
<input type="text" name="name" required="required">
</label>
<input type="submit" value="submit">
</form>
<div id="result"></div>
<script src="main.js"></script>
</body>
</html>
在新建个main.js
(function () {
$("form").on("submit", function (e) {
e.preventDefault();
// console.log(this["name"].value, this.action, this.method);
$.post(this.action, {name: this["name"].value}).done(function (data) {
$("#result").html(data);
}).fail(function (err) {
alert("无法连接服务器");
});
});
})();
在routes里面的index文件中添加如下代码
router.post("/hello", function (req, res) {
res.render("hello", {name: req.body.name});
});
再在views文件夹下添加hello.jade文件
html
head
title = "hello"
body
div hello #{name};
ok,运行即可
补充一下,send和render的区别
send:直接处理数据,作用:把数据传到<body>标签中,只能传来get,即传过来搜索框中的内容(处理的是直接的数据)
render:意为渲染,作用,把一个文件渲染后,传到<body>标签中,使用post处理过得数据,传送给那个文件,后在传到<body>标签中。(处理的是文件)
node服务器获取form表单的更多相关文章
- serialize可以获取form表单里面的数值
serialize属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- java:JavaScript2:(setTimeout定时器,history.go()前进/后退,navigator.userAgent判断浏览器,location.href,五种方法获取标签属性,setAttribute,innerHTML,三种方法获取form表单信息,JS表单验证,DOM对象,form表单操作)
1.open,setTimeout,setInterval,clearInterval,clearTimeout <!DOCTYPE> <html> <head> ...
- 5 获取Form表单取值
#form表达提交@app.route("/data",methods=['GET','POST']) #methods 让当前路由支持GET 和 POST 方式def data( ...
- jquery获取form表单内容以及绑定数据到form表单
在日常开发的过程中,难免会用到form表单,我们需要获取表单的数据保存到数据库,或者拿到后台的一串json数据,要将数据绑定到form表单上,这里我写了一个基于jquery的,formHelp插件,使 ...
- JS获取form表单数据
以下代码可放在一个js文件中,以便通用: //获取指定表单中指定标签对象 function getElements(formId, label) { var form = document.getEl ...
- 通过脚本获取form表单的数值而不是submit
jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化,如: <form action=" ...
- 3..jquery的ajax获取form表单数据
jq是对dom进行的再次封装.是一个js库,极大简化了js使用 jquery库在js文件中,包含了所有jquery函数,引用:<script src="jquery-1.11.1.mi ...
- 获取form表单默认提交的返回值
1.经常用form表单提交的小伙伴有没有发现,form表单默认的提交是没有返回值的,而且默认提交成功之后是跳转,跳转的action的路径,下面写一下默认的提交如何获取到form表单的返回值json,并 ...
- html基础:jquery的ajax获取form表单数据
jq是对dom进行的再次封装.是一个js库,极大简化了js使用 jquery库在js文件中,包含了所有jquery函数,引用:<script src="jquery-1.11.1.mi ...
随机推荐
- 思考之一——PM(Project Manager)
摘自:http://blog.csdn.net/moreevan/article/details/6697806
- Codeoforces 558 B. Duff in Love
// B. Duff in Love time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- SqlSever基础 datepart函数 返回现在几点了
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n!); (4)编写测试类E测试
package a; public interface InterfaceA { int method(int n); } package a; public class ClassA impleme ...
- 【leetcode❤python】119. Pascal's Triangle II
#-*- coding: UTF-8 -*-#杨辉三角返回给定行#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行class Solution(obj ...
- C# 多线程 Invoke BeginInvoke
Invoke在线程中等待Dispatcher调用指定方法,完成后继续下面的操作. BeginInvoke不必等待Dispatcher调用制定方法,直接继续下面的操作. 来自:百度知道 这个在线程中操作 ...
- jquery相对选择器,又叫context选择器,上下文选择器;find()与children()区别
jquery相对选择器有两个参数,jQuery函数的第二个参数可以指定DOM元素的搜索范围(即以第二个参数指定的内容为容器查找指定元素). 第二个参数的不同的类型,对应的用法如下表所示. 类型 用法 ...
- Dbcp2抛出org.apache.commons.dbcp2.LifetimeExceededException
三月 24, 2016 5:16:33 下午 org.apache.commons.dbcp2.BasicDataSource onSwallowException 警告: An internal o ...
- Linux常见问题的处理方法(长期更新)
一.使用sudo命令时xxx is not in the sudoers file. This incident will be reported. 1.su -,输入root的密码完成身份切换. 2 ...
- Topic Model
Topic Model 标签(空格分隔): 机器学习 \(\Gamma\)函数 \(\Gamma\)函数可以看做是阶乘在实数域上的推广,即: \(\Gamma(x) = \int_{0}^{+\inf ...