• form表单转化json对象


    利用 $.fn 可以让每一个jquery 对象都能直接使用这个方法。

    //form表单转化json对象
    $.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
    if (o[this.name]) {
    if (!o[this.name].push) {
    o[this.name] = [o[this.name]];
    }
    o[this.name].push(this.value || '');
    } else {
    o[this.name] = this.value || '';
    }
    });
    return o;
    };

    $.fn.serializeObjectNoNullAttr = function () {
    var data = this.serializeObject();
    for (var p in data) {
    if (data[p] == null || data[p] == '') {
    delete data[p];
    }
    }
    return data;
    };

    用法:
    $("#searchForm").serializeObjectNoNullAttr()
  • 相关阅读:
    Dice 5 ==> dice 7
    Dice 7 ==> dice 5
    100 floors 2 eggs
    Determine overlapping rectangles
    Product of integers
    Maximum profit of stocks
    样式表
    20170508
    2017年05月07
    The second day of school
  • 原文地址:https://www.cnblogs.com/daiwenru/p/8046046.html
Copyright © 2020-2023  润新知