jquery.serializeJSON
====================
Adds the method `.serializeJSON()` to [jQuery](http://jquery.com/) (or [Zepto](http://zeptojs.com/)) that serializes a form into a JavaScript Object, using the same format as the default Ruby on Rails request params.
Install
-------
Install with [bower](http://bower.io/) `bower install jquery.serializeJSON`, or [npm](https://www.npmjs.com/) `npm install jquery-serializejson`, or just download the [jquery.serializejson.js](https://raw.githubusercontent.com/marioizquierdo/jquery.serializeJSON/master/jquery.serializejson.js) script.
And make sure it is included after jQuery (or Zepto), for example:
```html
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.serializejson.js"></script>
```
Usage Example
-------------
HTML form (input, textarea and select tags supported):
```html
<form id="my-profile">
<!-- simple attribute -->
<input type="text" name="fullName" value="Mario Izquierdo" />
<!-- nested attributes -->
<input type="text" name="address[city]" value="San Francisco" />
<input type="text" name="address[state][name]" value="California" />
<input type="text" name="address[state][abbr]" value="CA" />
<!-- array -->
<input type="text" name="jobbies[]" value="code" />
<input type="text" name="jobbies[]" value="climbing" />
<!-- textareas, checkboxes ... -->
<textarea name="projects[0][name]">serializeJSON</textarea>
<textarea name="projects[0][language]">javascript</textarea>
<input type="hidden" name="projects[0][popular]" value="0" />
<input type="checkbox" name="projects[0][popular]" value="1" checked />
<textarea name="projects[1][name]">tinytest.js</textarea>
<textarea name="projects[1][language]">javascript</textarea>
<input type="hidden" name="projects[1][popular]" value="0" />
<input type="checkbox" name="projects[1][popular]" value="1"/>
<!-- select -->
<select name="selectOne">
<option value="paper">Paper</option>
<option value="rock" selected>Rock</option>
<option value="scissors">Scissors</option>
</select>
<!-- select multiple options, just name it as an array[] -->
<select multiple name="selectMultiple[]">
<option value="red" selected>Red</option>
<option value="blue" selected>Blue</option>
<option value="yellow">Yellow</option>
</select>
</form>
```
JavaScript:
```javascript
$('#my-profile').serializeJSON();
// returns =>
{
fullName: "Mario Izquierdo",
address: {
city: "San Francisco",
state: {
name: "California",
abbr: "CA"
}
},
jobbies: ["code", "climbing"],
projects: {
'0': { name: "serializeJSON", language: "javascript", popular: "1" },
'1': { name: "tinytest.js", language: "javascript", popular: "0" }
},
selectOne: "rock",
selectMultiple: ["red", "blue"]
}
```
The `serializeJSON` function returns a JavaScript object, not a JSON String. It should probably have been called `serializeObject`, or something like that, but those names were already taken.
To serialize into JSON, use the `JSON.stringify` method, that is available on all major [new browsers](http://caniuse.com/json).
To support old browsers, just include the [json2.js](https://github.com/douglascrockford/JSON-js) polyfill (as described on [stackoverfow](http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery)).
```javascript
var jsonString = JSON.stringify(obj);
```
Note that `.serializeJSON()` implememtation relies on jQuery's [.serializeArray()](https://api.jquery.com/serializeArray/) to grab the form attributes and then create the object using the names.
That means, it will serialize the inputs that are supported by `.serializeArray()`, that uses the standard W3C rules for [successful controls](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2) to determine which elements it should include. In particular, the included elements cannot be disabled and must contain a name attribute. No submit button value is serialized since the form was not submitted using a button. And data from file select elements is not serialized.
Parse values with :types
------------------------
Parsed values are **strings** by default. But you can force values to be parsed with specific types by appending the type with a colon.
```html
<form>
<input type="text" name="deftype" value=":string is the default (implicit) type"/>
<input type="text" name="text:string" value=":string type can still be used to overrid other parsing options"/>
<input type="text" name="excluded:skip" value="Use :skip to not include this field in the result"/>
<input type="text" name="numbers[1]:number" value="1"/>
<input type="text" name="numbers[1.1]:number" value="1.1"/>
<input type="text" name="numbers[other stuff]:number" value="other stuff"/>
<input type="text" name="bools[true]:boolean" value="true"/>
<input type="text" name="bools[false]:boolean" value="false"/>
<input type="text" name="bools[0]:boolean" value="0"/>
<input type="text" name="nulls[null]:null" value="null"/>
<input type="text" name="nulls[other stuff]:null" value="other stuff"/>
<input type="text" name="autos[string]:auto" value="text with stuff"/>
<input type="text" name="autos[0]:auto" value="0"/>
<input type="text" name="autos[1]:auto" value="1"/>
<input type="text" name="autos[true]:auto" value="true"/>
<input type="text" name="autos[false]:auto" value="false"/>
<input type="text" name="autos[null]:auto" value="null"/>
<input type="text" name="autos[list]:auto" value="[1, 2, 3]"/>
<input type="text" name="arrays[empty]:array" value="[]"/>
<input type="text" name="arrays[list]:array" value="[1, 2, 3]"/>
<input type="text" name="objects[empty]:object" value="{}"/>
<input type="text" name="objects[dict]:object" value='{"my": "stuff"}'/>
</form>
```
```javascript
$('form').serializeJSON();
// returns =>
{
"deftype": "default type is :string",
"text": ":string type overrides parsing options",
// excluded:skip is not included in the output
"numbers": {
"1": 1,
"1.1": 1.1,
"other stuff": NaN, // <-- Other stuff parses as NaN (Not a Number)
},
"bools": {
"true": true,
"false": false,
"0": false, // <-- "false", "null", "undefined", "", "0" parse as false
},
"nulls": {
"null": null, // <-- "false", "null", "undefined", "", "0" parse as null
"other stuff": "other stuff"
},
"autos": { // works as the parseAll option
"string": "text with stuff",
"0": 0, // <-- parsed as number
"1": 1, // <-- parsed as number
"true": true, // <-- parsed as boolean
"false": false, // <-- parsed as boolean
"null": null, // <-- parsed as null
"list": "[1, 2, 3]" // <-- array and object types are not auto-parsed
},
"arrays": { // <-- works using JSON.parse
"empty": [],
"not empty": [1,2,3]
},
"objects": { // <-- works using JSON.parse
"empty": {},
"not empty": {"my": "stuff"}
}
}
```
Types can also be specified with the `data-value-type` attribute, instead of the :type notation:
```html
<form>
<input type="text" name="anumb" data-value-type="number" value="1"/>
<input type="text" name="abool" data-value-type="boolean" value="true"/>
<input type="text" name="anull" data-value-type="null" value="null"/>
<input type="text" name="anauto" data-value-type="auto" value="0"/>
</form>
```
Options
-------
By default `.serializeJSON()`, with no options works like this:
* Values are always **strings** (unless appending :types to the input names)
* Unchecked checkboxes are ignored (as defined in the W3C rules for [successful controls](http://www.w3.or
没有合适的资源?快使用搜索试试~ 我知道了~
EDUSOHO-ct-21.4.5
共2000个文件
js:1773个
css:130个
vue:38个
2 下载量 16 浏览量
2023-11-11
20:37:48
上传
评论
收藏 92.04MB ZIP 举报
温馨提示
EDUSOHO-ct-21.4.5
资源推荐
资源详情
资源评论
收起资源包目录
EDUSOHO-ct-21.4.5 (2000个子文件)
antd.css 587KB
main.css 512KB
antd.min.css 455KB
admin.css 412KB
main-v2.css 149KB
bootstrap.css 137KB
es-icon.css 111KB
es-icon.css 107KB
bootstrap.css 105KB
web.css 84KB
main.css 57KB
main-orange.css 57KB
main-purple.css 57KB
main-red.css 57KB
main-purple-light.css 57KB
main-red-light.css 57KB
main-blue-light.css 57KB
main-blue.css 57KB
main-green-light.css 57KB
main-orange-light.css 57KB
main-green-light.css 28KB
main-purple-light.css 28KB
main-purple.css 28KB
main-blue-light.css 28KB
main-orange-light.css 28KB
ct-icon.css 28KB
main-orange.css 28KB
main-blue.css 28KB
main-red-light.css 28KB
main-red.css 28KB
select2-debug.css 21KB
admin_v2.css 20KB
select2.css 19KB
select2.css 16KB
admin.css 15KB
ui.css 12KB
fui.css 10KB
mobile.css 9KB
fui.min.css 7KB
admin.css 6KB
admin-main.css 5KB
group.css 4KB
treeview.css 3KB
style.css 3KB
perfect-scrollbar.css 3KB
default.css 3KB
railscasts.css 3KB
arta.css 2KB
monokai_sublime.css 2KB
obsidian.css 2KB
xcode.css 2KB
sunburst.css 2KB
googlecode.css 2KB
monokai.css 2KB
school_book.css 2KB
magula.css 2KB
far.css 2KB
zenburn.css 2KB
docco.css 2KB
brown_paper.css 2KB
course-wide.list.css 2KB
atelier-lakeside.light.css 2KB
dark.css 2KB
atelier-lakeside.dark.css 2KB
idea.css 2KB
atelier-seaside.light.css 2KB
atelier-seaside.dark.css 2KB
atelier-forest.light.css 2KB
atelier-forest.dark.css 2KB
atelier-heath.light.css 2KB
atelier-heath.dark.css 2KB
atelier-dune.light.css 2KB
atelier-dune.dark.css 2KB
github.css 2KB
pojoaque.css 2KB
foundation.css 2KB
solarized_dark.css 2KB
solarized_light.css 2KB
rainbow.css 2KB
tomorrow-night-blue.css 2KB
tomorrow-night.css 2KB
vs.css 2KB
tomorrow-night-eighties.css 2KB
tomorrow-night-bright.css 2KB
templates.css 2KB
paraiso.light.css 2KB
paraiso.dark.css 2KB
ir_black.css 2KB
jquery.autocomplete.css 2KB
article.css 1KB
tomorrow.css 1KB
scrollbar.css 1KB
toolbar.css 1KB
header-white.css 1KB
base.css 1KB
mono-blue.css 961B
ascetic.css 853B
mobile.css 834B
browser-upgrade.css 795B
colordialog.css 792B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
高小了兔
- 粉丝: 691
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- tinymce 多图片批量上传插件
- Virtualized Hadoop Performance with VMware vSphere 6 on Servers
- 基于java开发的驾校学员信息管理系统
- 电气类117、变电站真实巡检电力设备检测数据集(7500多张+15类+YOLO格式txt标签)-第二部分
- 智能图像处理应用案例介绍:开发基于Java的智能图像处理应用,包括图像识别、图像增强等功能
- HTML 和 JavaScript 创建一个端午节的祝福页面.docx
- 电气类、117.变电站真实巡检电力设备检测数据集(7500多张+15类+YOLO格式txt标签)-第一部分
- IMG_20180720_124347_01.jpg
- IMG20240621095811.jpg
- jQuery 库.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功