本文介绍如何在原生JavaScript中处理多个表单的数据提交。通过复用《JavaScript高级程序设计》中的serialize()函数,并改造为返回对象格式,结合JSON.stringify()将多个表单数据转换为JSON字符串传递给后端。重点讲解了使用Object.defineProperty时设置enumerable:true的必要性,以确保数据正确序列化。
多表单序列化背景与原生方案
本文通过具体实例,讲解在JavaScript中处理多个表单数据提交时serialize()函数的应用方法,供开发者参考。
在实际开发过程中,经常遇到需要同时传递多个表单数据的场景。
进行多表单数据传递的主要目的是实现数据分组,从而更便于后续的数据维护与管理。
为了减少对jQuery的依赖,采用原生JavaScript函数解决该问题是最优选择。源自《JavaScript高级程序设计》的serialize()函数正是解决此问题的有效方案,其基础实现如下:
基础serialize()函数实现
function serialize(form){
var parts = [],
field = null,
i,
len,
j,
optLen,
option,
optValue;
for (i=0, len=form.elements.length; i < len; i++){
field = form.elements[i];
switch(field.type){
case "select-one":
case "select-multiple":
if (field.name.length){
for (j=0, optLen = field.options.length; j < optLen; j++){
option = field.options[j];
if (option.selected){
optValue = "";
if (option.hasAttribute){
optValue = (option.hasAttribute("value") ? option.value : option.text);
} else {
optValue = (option.attributes["value"].specified ? option.value : option.text);
}
parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));
}
}
}
break;
case undefined: //fieldset
case "file": //file input
case "submit": //submit button
case "reset": //reset button
case "button": //custom button
break;
case "radio": //radio button
case "checkbox": //checkbox
if (!field.checked){
break;
}
/* falls through */
default:
//don't include form fields without names
if (field.name.length){
parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));
}
}
}
console.log(parts);
return parts.join("&");
}
熟悉《JavaScript高级程序设计》的开发者对该函数应当不陌生;
然而,若传递目标是对象而非字符串,原函数显然无法满足需求。针对这一开发场景,我们对函数进行了改造,修改后的代码如下:
对象格式改造与JSON序列化
function serialize(form){
var parts = {},
field = null,
i,
len,
j,
optLen,
option,
optValue;
for (i=0, len=form.elements.length; i < len; i++){
field = form.elements[i];
switch(field.type){
case "select-one":
case "select-multiple":
if (field.name.length){
for (j=0, optLen = field.options.length; j < optLen; j++){
option = field.options[j];
if (option.selected){
optValue = "";
if (option.hasAttribute){
optValue = (option.hasAttribute("value") ? option.value : option.text);
} else {
optValue = (option.attributes["value"].specified ? option.value : option.text);
}
//将数据改为对象形式
Object.defineProperty(parts, encodeURIComponent(field.name), {
value:encodeURIComponent(optValue),
enumerable:true //为真表示可枚举,只有可枚举才能出现在JSON.stringify()转换的json数据中
});
}
}
}
break;
case undefined: //fieldset
case "file": //file input
case "submit": //submit button
case "reset": //reset button
case "button": //custom button
break;
case "radio": //radio button
case "checkbox": //checkbox
if (!field.checked){
break;
}
/* falls through */
default:
//don't include form fields without names
if (field.name.length){
//构建对象
Object.defineProperty(parts, encodeURIComponent(field.name), {
value:encodeURIComponent(field.value),
enumerable:true //为真表示可枚举,只有可枚举才能出现在JSON.stringify()转换的json数据中
});
}
}
}
console.log(parts);
return parts;
}
改造后,表单数据以对象形式展示。若涉及多个表单,可将各表单保存至数组中,利用JSON.stringify()将其转换为JSON格式,再发送给后端接口;
注意事项:
在使用Object.defineProperty创建对象属性时,必须设置enumerable:true。否则,该属性无法出现在JSON.stringify()转换生成的JSON数据中。这是由JSON.stringify()的机制所决定的。
感兴趣的用户可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试上述代码的运行效果。
更多关于JavaScript相关内容可查看本站专题:《JavaScript操作DOM技巧总结》、《JavaScript页面元素操作技巧总结》、《JavaScript事件相关操作与技巧大全》、《JavaScript查找算法技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript错误与调试技巧总结》
希望本文所述内容对大家的JavaScript程序设计有所帮助。