將任意一個jQuery對象進行表單序列化,免除了提交請求時大量拼字表單資料的煩惱,支援索引值對<name&value>格式和JSON格式。

來源:互聯網
上載者:User

標籤:

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格式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.