http://zhengxinlong.iteye.com/blog/848712

将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name&value>格式和JSON格式。

/// <reference name="jquery.js" description="1.3.2版本以上" />

/*!* 扩展jQuery表单序列化函数:{ Version: 1.2, Author: Eric.Zheng, CreateDate: 2010-12-21 }
*
* 消除了jQuery.serialize()只能对form进行序列化的局限
* 该插件可以对任意jQuery对象进行序列化
* 返回数据格式有两种:1.<name&value>(默认) 2.json
*
* 调用方法:$(dom).form_serialize(dataType);
* 参数(可省略):dataType: 默认为html,即返回数据格式为<name&value>;若要返回json格式,则dataType = json;
* 返回数据:序列化表单数据
*
* BUG修复:修复了1.0版本中,多个Dom元素使用同一个name属性时,获取的数据有缺失。
*
**/
(function ($) {
var formJson = {};
var currentForm = null; $.fn.form_serialize = function (dataType) {
currentForm = $(this);
formJson = {};
var doms = currentForm.find('[name]');
$.each(doms, function (index, dom) {
var domName = $(dom).attr('name');
if (!formJson[domName]) {
formJson[domName] = { Name: domName, Type: $(dom).attr('type'), Doms: currentForm.find('[name=' + domName + ']') };
}
});
return getResult(dataType);
}; var getResult = function (dataType) {
var d = {
toJson: function () {
var data = {};
$.each(formJson, function (key, json) {
data[key] = getVal(json);
});
return data;
},
toString: function () {
var val = '';
var index = 0;
$.each(formJson, function (key, json) {
var prefix = '&';
if (index == 0) prefix = '';
index++;
val += prefix + key + '=' + getVal(json);
});
return val;
}
};
return dataType == 'json' ? d.toJson() : d.toString();
} var getVal = function (json) {
var methods = {
getDefaultVal: function (dom) {
return $(dom).val();
},
getSelectVal: function (dom) {
var val = '';
var selectType = $(dom).attr('type');
if (selectType == 'select-multiple') {
var items = $(dom).val();
if (items == null) return '';
for (var i = 0; i < items.length; i++) {
val += i == 0 ? items[i] : (',' + items[i]);
}
return val;
} else {
return $(dom).val();
}
},
getRadioVal: function (dom) {
return $(dom).attr('checked') ? $(dom).val() : null;
},
getCheckBoxVal: function (dom) {
return methods.getRadioVal(dom);
}
}; var dispacher = function (type, dom) {
switch (type) {
case 'text':
case 'password':
case 'hidden':
case 'textarea':
return methods.getDefaultVal(dom);
case 'select-one':
case 'select-multiple':
return methods.getSelectVal(dom);
case 'radio':
return methods.getRadioVal(dom);
case 'checkbox':
return methods.getCheckBoxVal(dom);
default:
return '';
}
}; var domType = json.Type;
var doms = $(json.Doms);
var count = doms.length;
if (count > 1) {
var val = '';
var index = 0;
for (var i = 0; i < count; i++) {
var v = dispacher(domType, doms.eq(i));
if (v == '' || v == null || v == undefined)
continue;
val += index++ == 0 ? dispacher(domType, doms.eq(i)) : (',' + dispacher(domType, doms.eq(i)));
}
return val;
} else {
return dispacher(domType, doms);
}
};
})(jQuery);

将任意一个jQuery对象进行表单序列化,免除了提交请求时大量拼写表单数据的烦恼,支持键值对<name&value>格式和JSON格式。的更多相关文章

  1. 第一百五十九节,封装库--JavaScript,表单序列化结合ajax提交数据

    封装库--JavaScript,表单序列化结合ajax提交数据 封装库,表单序列化方法 /** xu_lie_biao_dan()方法,表单序列化方法,将自动获取指定表单里面的各项字段name值和va ...

  2. (转)jquery serialize表单序列化,当radio或checkbox 未选中时,没有序列化到对象中的原因分析和解决方案 - ghostsf

    相信很多人都用过jq的表单序列化serialize()方法,因为这能很方便地帮你把表单里所有的非禁用输入控件序列化为 key/value 对象,不需要你再去一个个地拼接参数了. 这是一个很好用的函数, ...

  3. jQuery实现form表单序列化转换为json对象功能示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. jQuery form插件的使用--用 formData 参数校验表单,验证后提交(简单验证).

    Form Plugin API 里提供了很多有用的方法可以让你轻松的处理表单里的数据和表单的提交过程. 测试环境:部署到Tomcat中的web项目. 一.引入依赖js <script src=& ...

  5. Jquery表单序列化和AJAX全局事件

    Jquery表单序列化 1.必须放在form标签内: 2.控件必须有name属性: 3.控件的value值会提交到服务器: 如: <form id="form1"> & ...

  6. 判断一个jquery对象是否为空

    今天用jquery $获取一个jquery对象.$("#id") 然后用判断这个对象是否存在,id不存在的时候,判断这个是否存在, if($("#id")) 始 ...

  7. 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. $ 和getElementId的区别 / 一个jquery对象的原型

    请说出 div 和 $div 的联系和区别 区别 div 返回一个HTML DOM Object $div 返回一个 jQuery Object, 两者不等价 $div是包装了dom对象后产生的,无法 ...

  9. jquery ajax(5)form表单序列化

    form表单序列化<script type="text/javascript"> $(function(){ $("#send").click(fu ...

随机推荐

  1. Jenkins+GitHub+Xcode+fir搭了一个持续集成环境

    enkins+GitHub+Xcode+fir搭了一个持续集成环境 字数826 阅读5699 评论44 喜欢49 原文链接 Coding Duck 今天用Jenkins+GitHub+Xcode+fi ...

  2. MIC中offload语法总结

    MIC中offload的用法如下: #pragma offload specifier [,specifier...]specifier可以填入的选项为:target 例:taget(mic:0)if ...

  3. 第一篇: Ansible 介绍

    应用场景:   BOSS:运维帮忙把所有的服务器tomcat 重启一下,谢谢!(tomcat 服务有2K台) 运维:………… 运维:  啪啪啪啪啪啪啪啪..........(键盘的声音响彻办公室) B ...

  4. IE下object元素遮挡div表单

    目前遇到这样的一个问题: 我用ActiveX插件做了一个C#的播放器,要将这个插件放到web浏览器中,然后可以通过前台页面来控制视频的播放,暂停还有回放,这个时候发现object的onclick事件无 ...

  5. android-support-v4.jar异常解决方法

    1.当一个项目引入其他library项目时,会出现android-support-v4.jar冲突问题: 解决:将library项目中的android-support-v4.jar更新到最新,方法右键 ...

  6. iOS 动画基础总结篇

    iOS 动画基础总结篇   动画的大体分类(个人总结可能有误) 分类.png UIView 动画 属性动画 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...

  7. 我的Android进阶之旅------>android中getLocationInWindow 和 getLocationOnScreen的区别

    View.getLocationInWindow(int[] location) 一个控件在其父窗口中的坐标位置 View.getLocationOnScreen(int[] location) 一个 ...

  8. 丢失vcruntime140.dll

    我在php7安装yaf时报了标题所提示的错误信息. 解决方案是:下载vc++2015 并安装 链接如下:https://www.microsoft.com/zh-cn/download/confirm ...

  9. Bootstrap学习2--组件-列表组

    备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html 1.列表组 列表组是Bootstrap框架新增的一个组件 ...

  10. matlab常用的一些程序和功能

    ~ 去除误匹配算法(matlab) 1.ransac算法 [tform,matchedPoints1,matchedPoints2] = ...    estimateGeometricTransfo ...