$.ajax()使用serialize()提交form数据
jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化,如:
<form action="">
First name: <input type="text" name="FirstName" value="Bill" /><br />
Last name: <input type="text" name="LastName" value="Gates" /><br />
</form>
$(document).ready(function(){
console.log($("form").serialize()); // FirstName=Bill&LastName=Gates
});
这样,我们就可以把序列化的值传给ajax()作为url的参数,轻松使用ajax()提交form表单了,而不需要一个一个获取表单中的值然后传给ajax(),举例如下:
$.ajax({
type: 'post',
url: 'your url',
data: $("form").serialize(),
success: function(data) {
// your code
}
});
使用$.post()、$.get()和$.getJSON()也是一样的:
$.post('your url', $("form").serialize(), function(data) {
// your code
}
});
$.get('your url', $("form").serialize(), function(data) {
// your code
}
});
$.getJSON('your url', $("form").serialize(), function(data) {
// your code
}
});
随机推荐
- Ubuntu14.04安装和配置Tomcat8.0.12(转)
Ubuntu14.04长的好看,所以一时间很感兴趣,研究各种软件的安装和开发环境的配置.今天先把安装的tomcat 8.0.12的教程分享给大家.如果你需要,请收藏!!! 工具/原料 系统环境:U ...
- 项目重新部署后报The attribute required is undefined for the annotation type XmlElementRef
在另外一台机器上部署项目,项目导进Eclipse中发现有异常 public class BooleanFeatureType extends FeatureBaseType{ @XmlElementR ...
- ubuntu虚拟机安装
安装VMware Workstation 下载ubuntu镜像: http://www.ubuntu.com/download/ git for Windows下载: http://msysgit.g ...
- iOS - (两个APP之间的跳转)
一个程序若要跳到另一个程序.需要在目标程序的plist文件里面修改: 打开info.plist,添加一项URL types 展开URL types,再展开Item0,将Item0下的URL ident ...
- REST Security with JWT using Java and Spring Security
Security Security is the enemy of convenience, and vice versa. This statement is true for any system ...
- chart crash
* thread #155: tid = 0x1fcc10, 0x0000000107626745 gpxj`static gpxj.ChartUtils.decimals (gpxj.ChartUt ...
- Java常用锁机制简介
在开发Java多线程应用程序中,各个线程之间由于要共享资源,必须用到锁机制.Java提供了多种多线程锁机制的实现方式,常见的有synchronized.ReentrantLock.Semaphore. ...
- 部署基于JDK的webservice服务类
部署服务端 两个注解(@WebService @WebMethod).一个类(Endpoint) 首先新建JAVA工程ws-server 目录结构如下 在工程里新建一个接口,申明一个方法. packa ...
- IntelliJ IDEA 常用设置讲解2
IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 如上图 Gif 所示, ...
- 与PostgreSQL相关的工具
Pentaho Data Integration(kettle):一个优秀的抽取.转换.加载(Extract Transform and Load,ETL)工具 Pentaho Report Ser ...