jQuey表单序列化

2024-02-17 19:48
文章标签 表单 序列化 jquey

本文主要是介绍jQuey表单序列化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前端在处理含有大量数据提交的表单时,除了使用Form直接提交刷新页面之外,经常碰到的需求是收集表单信息成数据对象,Ajax提交。

而在处理复杂的表单时,需要一个一个区手动判断处理字段值,显得非常麻烦。接下来介绍的插件将解决这个问题。

关于serializeJSON

使用jquery.serializeJSON,可以在基于jQuery或者Zepto的页面中,调用 .serializeJSON() 方法来序列化form表单的数据成JS对象。

使用

只需要在jQuery或者Zepto时候引入即可

 

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.serializejson.js"></script>

 

示例

 

HTML form(支持inputtextareaselect等标签)

<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

$('#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"]
}

 

serializeJSON方法返回一个JS对象,并非JSON字符串。可以使用 JSON.stringify 转换成字符串(注意IE8兼容性)。

JavaScript权威指南(第6版)(中文版) http://www.gooln.com/document/452.html

var jsonString = JSON.stringify(obj);

 

指定数据类型

获取到的属性值一般是字符串,可以通过HTML指定类型 : type 进行强制转换。

<form><input type="text" name="notype"           value="default type is :string"/><input type="text" name="string:string"    value=":string type overrides parsing options"/><input type="text" name="excluded:skip"    value="Use :skip to not include this field in the result"/><input type="text" name="number[1]:number"           value="1"/><input type="text" name="number[1.1]:number"         value="1.1"/><input type="text" name="number[other stuff]:number" value="other stuff"/><input type="text" name="boolean[true]:boolean"      value="true"/><input type="text" name="boolean[false]:boolean"     value="false"/><input type="text" name="boolean[0]:boolean"         value="0"/><input type="text" name="null[null]:null"            value="null"/><input type="text" name="null[other stuff]:null"     value="other stuff"/><input type="text" name="auto[string]:auto"          value="text with stuff"/><input type="text" name="auto[0]:auto"               value="0"/><input type="text" name="auto[1]:auto"               value="1"/><input type="text" name="auto[true]:auto"            value="true"/><input type="text" name="auto[false]:auto"           value="false"/><input type="text" name="auto[null]:auto"            value="null"/><input type="text" name="auto[list]:auto"            value="[1, 2, 3]"/><input type="text" name="array[empty]:array"         value="[]"/><input type="text" name="array[list]:array"          value="[1, 2, 3]"/><input type="text" name="object[empty]:object"       value="{}"/><input type="text" name="object[dict]:object"        value='{"my": "stuff"}'/>
</form>

 

$('form').serializeJSON();// returns =>
{"notype": "default type is :string","string": ":string type overrides parsing options",// :skip type removes the field from the output"number": {"1": 1,"1.1": 1.1,"other stuff": NaN, // <-- Other stuff parses as NaN (Not a Number)},"boolean": {"true": true,"false": false,"0": false, // <-- "false", "null", "undefined", "", "0" parse as false},"null": {"null": null, // <-- "false", "null", "undefined", "", "0" parse as null"other stuff": "other stuff"},"auto": { // 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},"array": { // <-- works using JSON.parse"empty": [],"not empty": [1,2,3]},"object": { // <-- works using JSON.parse"empty": {},"not empty": {"my": "stuff"}}
}

 

数据类型也可以指定在 data-value-type 属性中,代替 :type 标记。

<form><input type="text" name="number[1]"     data-value-type="number"  value="1"/><input type="text" name="number[1.1]"   data-value-type="number"  value="1.1"/><input type="text" name="boolean[true]" data-value-type="boolean" value="true"/><input type="text" name="null[null]"    data-value-type="null"    value="null"/><input type="text" name="auto[string]"  data-value-type="auto"    value="0"/>
</form>

 

options配置

默认配置

  • Values始终为字符串(除非在input names使用:types )
  • Keys始终为字符串(默认不自动检测是否需要转换为数组)
  • 未选择的checkboxes会被忽略
  • disabledelements会被忽略

自定义配置

写法                                 释义
checkboxUncheckedValue: string      针对未勾选的checkboxes,设定值
parseBooleans: true                 自动检测转换”true”、”false”为布尔值true、false
parseNumbers: true                  自动检测转换”1″、”33.33″、”-44″为数字1、33.33、-44
parseNulls: true                    自动检测字符串”null”为null
parseAll: true                      自动检测转换以上类型的字符串
parseWithFunction: function         自定义转换函数 function(value, name){return parsedValue}
customTypes: {}                     自定义:types覆盖默认types,如{type: function(value){…}}
defaultTypes: {defaultTypes}        重新定义所有的:types,如{type: function(value){…}}
useIntKeysAsArrayIndex: true        当keys为整数时,将序列化为数组

 

 包含未勾选的checkboxes

serializeJSON 支持 checkboxUncheckedValue 配置,或者可以在checkboxes添加 data-unchecked-value 属性。

默认方法:

 

<form><input type="checkbox" name="check1" value="true" checked/><input type="checkbox" name="check2" value="true"/><input type="checkbox" name="check3" value="true"/>
</form>
$('form').serializeJSON();// returns =>
{'check1': 'true'} // Note that check2 and check3 are not included because they are not checked

 

上面的写法会忽略未勾选的复选框。如果需要包含,则可以使用以下方法:

1. 配置checkboxUncheckedValue

 

$('form').serializeJSON({checkboxUncheckedValue: "false"});// returns =>
{'check1': 'true', check2: 'false', check3: 'false'}

 

2. 添加data-unchecked-value属性

<form id="checkboxes"><input type="checkbox" name="checked[bool]"  value="true" data-unchecked-value="false" checked/><input type="checkbox" name="checked[bin]"   value="1"    data-unchecked-value="0"     checked/><input type="checkbox" name="checked[cool]"  value="YUP"                               checked/><input type="checkbox" name="unchecked[bool]"  value="true" data-unchecked-value="false" /><input type="checkbox" name="unchecked[bin]"   value="1"    data-unchecked-value="0" /><input type="checkbox" name="unchecked[cool]"  value="YUP" /> <!-- No unchecked value specified -->
</form>

 

$('form#checkboxes').serializeJSON(); // Note no option is used// returns =>
{'checked': {'bool':  'true','bin':   '1','cool':  'YUP'},'unchecked': {'bool': 'false','bin':  '0'// Note that unchecked cool does not appear, because it doesn't use data-unchecked-value}
}

 

自动检测转换类型

默认的类型为字符串 :string ,可以通过配置转换为其它类型

 

$('form').serializeJSON({parseNulls: true, parseNumbers: true});// returns =>
{"bool": {"true": "true", // booleans are still strings, because parseBooleans was not set"false": "false",}"number": {"0": 0, // numbers are parsed because parseNumbers: true"1": 1,"2.2": 2.2,"-2.25": -2.25,}"null": null, // "null" strings are converted to null becase parseNulls: true"string": "text is always string","empty": ""
}

 

在极少数情况下,可以使用自定义转换函数

 

var emptyStringsAndZerosToNulls = function(val, inputName) {if (val === "") return null; // parse empty strings as nullsif (val === 0)  return null; // parse 0 as nullreturn val;
}$('form').serializeJSON({parseWithFunction: emptyStringsAndZerosToNulls, parseNumbers: true});// returns =>
{"bool": {"true": "true","false": "false",}"number": {"0": null, // <-- parsed with custom function"1": 1,"2.2": 2.2,"-2.25": -2.25,}"null": "null","string": "text is always string","empty": null // <-- parsed with custom function
}

 

自定义类型

可以使用 customTypes 配置自定义类型或者覆盖默认类型($.serializeJSON.defaultOptions.defaultTypes

 

<form><input type="text" name="scary:alwaysBoo" value="not boo"/><input type="text" name="str:string"      value="str"/><input type="text" name="number:number"   value="5"/>
</form>
$('form').serializeJSON({customTypes: {alwaysBoo: function(str) { // value is always a stringreturn "boo";},string: function(str) { // all strings will now end with " override"return str + " override";}}
});// returns =>
{"scary": "boo",        // <-- parsed with type :alwaysBoo"str": "str override", // <-- parsed with new type :string (instead of the default)"number": 5,           // <-- the default :number still works
}

 

忽略空表单字段

// Select only imputs that have a non-empty value
$('form :input[value!=""]').serializeJSON();// Or filter them from the form
obj = $('form').find('input').not('[value=""]').serializeJSON();// For more complicated filtering, you can use a function
obj = $form.find(':input').filter(function () {return $.trim(this.value).length > 0}).serializeJSON();

使用整数keys作为数组的顺序

使用useIntKeyAsArrayIndex配置

 

<form><input type="text" name="arr[0]" value="foo"/><input type="text" name="arr[1]" value="var"/><input type="text" name="arr[5]" value="inn"/>
</form>

 

按照默认的方法,结果为:

 

$('form').serializeJSON();// returns =>
{'arr': {'0': 'foo', '1': 'var', '5': 'inn' }}

 

使用useIntKeyAsArrayIndex可以将记过转换为数组并制定顺序

$('form').serializeJSON({useIntKeysAsArrayIndex: true});// returns =>
{'arr': ['foo', 'var', undefined, undefined, undefined, 'inn']}

 

默认配置Defaults

所有的默认配置均定义在 $.serializeJSON.defaultOptions,可以进行修改。

$.serializeJSON.defaultOptions.parseAll = true; // parse booleans, numbers and nulls by default$('form').serializeJSON(); // No options => then use $.serializeJSON.defaultOptions// returns =>
{"bool": {"true": true,"false": false,}"number": {"0": 0,"1": 1,"2.2": 2.2,"-2.25": -2.25,}"null": null,"string": "text is always string","empty": ""
}

这篇关于jQuey表单序列化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/718806

相关文章

HTML提交表单给python

python 代码 from flask import Flask, request, render_template, redirect, url_forapp = Flask(__name__)@app.route('/')def form():# 渲染表单页面return render_template('./index.html')@app.route('/submit_form',

form表单提交编码的问题

浏览器在form提交后,会生成一个HTTP的头部信息"content-type",标准规定其形式为Content-type: application/x-www-form-urlencoded; charset=UTF-8        那么我们如果需要修改编码,不使用默认的,那么可以如下这样操作修改编码,来满足需求: hmtl代码:   <meta http-equiv="Conte

js异步提交form表单的解决方案

1.定义异步提交表单的方法 (通用方法) /*** 异步提交form表单* @param options {form:form表单元素,success:执行成功后处理函数}* <span style="color:#ff0000;"><strong>@注意 后台接收参数要解码否则中文会导致乱码 如:URLDecoder.decode(param,"UTF-8")</strong></span>

前端form表单+ifarme方式实现大文件下载

// main.jsimport Vue from 'vue';import App from './App.vue';import { downloadTokenFile } from '@/path/to/your/function'; // 替换为您的函数路径// 将 downloadTokenFile 添加到 Vue 原型上Vue.prototype.$downloadTokenF

react笔记 8-21 约束性 表单

1、约束性组件和非约束性组件 非约束性组件<input type="text" name="" defaultValue={this.state.msg}></input>这里他的value是用户输入的值 并没有执行操作 只是获取到了msg的值 用户输入不会改变数据非约束性组件需要使用defaultValue获取数据 否则会报错约束性组件<input type="text

vue2实践:第一个非正规的自定义组件-动态表单对话框

前言 vue一个很重要的概念就是组件,作为一个没有经历过前几代前端开发的我来说,不太能理解它所带来的“进步”,但是,将它与后端c++、java类比,我感觉,组件就像是这些语言中的类和对象的概念,通过封装好的组件(类),可以通过挂载的方式,非常方便的调用其提供的功能,而不必重新写一遍实现逻辑。 我们常用的element UI就是由饿了么所提供的组件库,但是在项目开发中,我们可能还需要额外地定义一

js操作Dom节点拼接表单及ajax提交表单

有时候我们不希望html(jsp、vm)中有创建太多的标签(dom节点),所以这些任务都由js来做,下面提供套完整的表单提交流程,只需要在html中添加两个div其余的都由js来做吧。下面原生代码只需略微修改就能达到你想要的效果。 1、需要创建表单的点击事件 <a href="javascript:void(0);"onclick="changeSettleMoney('$!doctor.do

JSP 简单表单显示例子

<html><!--http://localhost:8080/test_jsp/input.html --><head><meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"><title>input页面</title></head><body><form action="input.jsp" method

Python---文件IO流及对象序列化

文章目录 前言一、pandas是什么?二、使用步骤 1.引入库2.读入数据总结 前言 前文模块中提到加密模块,本文将终点介绍加密模块和文件流。 一、文件流和IO流概述         在Python中,IO流是用于输入和输出数据的通道。它可以用于读取输入数据或将数据写入输出目标。IO流可以是标准输入/输出流(stdin和stdout),也可以是文件流,网络流等。

在幼儿园管理系统中,会议管理申请会议模块:添加会议记录(提交表单)的时候报:404错误!

在幼儿园管理系统(spring MVC)中,会议管理>申请会议模块:添加会议记录的时候报:404错误!不知道为啥找不到,一开始感觉一头雾水,怎么会出现404页面找不到错误那,又检查action,controller等这也没错啊!怎么出现404错误那。经过询问和查找,终于找到原因了。 原因是:添加的有时间字段。 代码: @InitBinder public void in