2

我有一个带有简单表单的简单页面:

<div data-role="page" id="main">
    <div data-role="header">
        <h1>Page Title</h1>
    </div>
    <div data-role="content">
        <form id="add_form">
            <input type="text" name="name" placeholder="Name" />
            <textarea name="description" placeholder="Description"></textarea>
            <button data-role="button" id="cancel-form" >Cancel</button>
            <input type="submit" value="Add" />
        </form>
    </div>
    <div data-role="footer">
        <h4>Page Footer</h4>
    </div>
</div>

然后

$(document).on( "pageshow", "#main", function() {
    new MAIN();
});


var MAIN = (function () {
    function MAIN() {
        $("#add_form").submit(function(){
            console.log($(this).serialize());
            return false;
        });
    }

    return MAIN;
})();

问题是我得到了name=&name=test&description=&description=some content

基本上在序列化字符串中重复..

jQuery页面上它说:

Warning: selecting both the form and its children will cause duplicates in the serialized string.

但我没有那样做,是吗?

关于这个问题的任何想法?

编辑:

我找到了一种可行的解决方案

var MAIN = (function () {
    function MAIN() {
        var _this = this;
        $("#add_form").submit(function(){
            console.log(_this.serializeObject($(this).serializeArray()));
            return false;
        });
    }

    MAIN.prototype.serializeObject = function(a) {
        var o = {};
        $.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;
    };

    return MAIN;
})();

这将 console.logObject {name: "werwerwer", description: "erewwewerwerw"}

4

1 回答 1

-1
var MAIN = (function () {
    function MAIN() {
        var _this = this;
        $("#add_form").submit(function(){
            console.log(_this.serializeObject($(this).serializeArray()));
            return false;
        });
    }

    MAIN.prototype.serializeObject = function(a) {
        var o = {};
        $.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;
    };

    return MAIN;
})();
于 2013-02-17T19:05:14.897 回答