---
title: "【转】jQuery ajax()使用serialize()提交form数据"
author: "wangyanlong0107"
date: "2018-12-19T04:22:41Z"
tags: []
categories: ["Web前端"]
slug: "jquery-ajax-serialize-form"
draft: false
lastmod: "2018-12-19T04:22:41Z"
id: 2435395
url: "https://bigdragon.iteye.com/blog/2435395"
---

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()，举例如下：

[![复制代码](https://common.cnblogs.com/images/copycode.gif)]()

```
$.ajax({
    type: 'post',
    url: 'your url',
    data: $("form").serialize(),
    success: function(data) {
        // your code
    }
});
```

[![复制代码](https://common.cnblogs.com/images/copycode.gif)]()

使用$.post()、$.get()和$.getJSON()也是一样的：

[![复制代码](https://common.cnblogs.com/images/copycode.gif)]()

```
$.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
    }
});
```

[![复制代码](https://common.cnblogs.com/images/copycode.gif)]()

[]()