zoukankan      html  css  js  c++  java
  • [备忘]js表单序列化代码

    function serialize(form) {
        var parts = [],
                elems = form.elements,
                i = 0,
                len = elems.length,
                filed = null;
        for (; i < len; i++) {
            filed = elems[i];
            switch (filed.type) {
                case "select-one":
                case "select-multiple":
                    if (filed.name.length) {
                        var j = 0,
                                opt,
                                optLen = filed.options.length;
                        for (; j < optLen; j++) {
                            opt = filed.options[j];
                            if (opt.selected) {
                                parts.push(encodeURIComponent(filed.name) + "=" + encodeURIComponent(opt.value));
                            }
                        }
                    }
                    break;
                case undefined:
                case "submit":
                case "reset":
                case "file":
                case "button":
                    break;
                case "radio":
                case "checkbox":
                    if (!filed.checked) {
                        break;
                    }
                default:
                    if (filed.name.length && filed.value) {
                        parts.push(encodeURIComponent(filed.name) + "=" + encodeURIComponent(filed.value));
                    }
            }
        }
        return parts.join("&");
    }

    使用实例:

    var data = serialize(document.getElementById("form"));
  • 相关阅读:
    7段数码管绘制
    画五角星
    绘制正方形
    蟒蛇的绘制
    玫瑰花
    小猪佩奇
    数列求和
    水仙花数
    鸡兔同笼
    画国际象棋盘
  • 原文地址:https://www.cnblogs.com/liqiujiong/p/10536496.html
Copyright © 2011-2022 走看看