首页 新闻 会员 周边

谁能给我解释下serialize,具体的注释

0
悬赏园豆:30 [已解决问题] 解决于 2015-02-05 15:20

serialize:function (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:

            //字段集

        case "file":

            //文件输入

        case "submit":

            //提交按钮

        case "reset":

            //重置按钮

        case "button":

            //自定义按钮

            break;

        case "radio":

            //单选按钮

        case "checkbox":

            //复选框

            if (!field.checked) {

                break;

            }

            /* 执行默认操作 */

        default:

            if (field.name.length) {

                parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));

            }

        }

    }

    return parts.join("&");

},

sunxd的主页 sunxd | 初学一级 | 园豆:4
提问于:2015-02-04 16:29
< >
分享
最佳答案
0

遍历表单元素,将输入控件收集起来,进行name=value这种拼接。

收获园豆:30
幻天芒 | 高人七级 |园豆:37175 | 2015-02-04 16:33

不懂。。

sunxd | 园豆:4 (初学一级) | 2015-02-05 13:12

@sunxd: 这...看看js的东东~这个比较基本。

幻天芒 | 园豆:37175 (高人七级) | 2015-02-05 13:17

@sunxd: 

serialize:function (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": //如果是多选select
            //如果元素有name属性
            if (field.name.length) {
                //遍历可选项
                for (j = 0, optLen = field.options.length; j < optLen;   j++) {
                    option = field.options[j];
                    //如果项是选中的
                    if (option.selected) {
                        optValue = "";
                        //同时有value值(这个if...else...是为了兼容浏览器的)
                        if (option.hasAttribute) {
                            optValue = (option.hasAttribute("value") ? option.value : option.text);
                        } else {
                            optValue = (option.attributes["value"].specified ? option.value : option.text);
                        }
                        //将选择的项目拼接为name=value的形式,并加入到parts中。
                        parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));
                    }
                }
            }
            break;
        case undefined:
            //字段集
        case "file":
            //文件输入
        case "submit":
            //提交按钮
        case "reset":
            //重置按钮
        case "button":
            //自定义按钮
            break;
        case "radio":
            //单选按钮
        case "checkbox":
            //复选框
            if (!field.checked) {
                break;
            }
            /* 执行默认操作 */
        default:
            if (field.name.length) {
                parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));
            }
        }
    }
    //返回表单元素的name=value拼接的值,用&连接
    return parts.join("&");
},
幻天芒 | 园豆:37175 (高人七级) | 2015-02-05 13:53

@幻天芒: 谢谢啦

sunxd | 园豆:4 (初学一级) | 2015-02-05 14:31

@sunxd: :)

幻天芒 | 园豆:37175 (高人七级) | 2015-02-05 15:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册