シンプルなフォームのシンプルなページがあります。
<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.logになりますObject {name: "werwerwer", description: "erewwewerwerw"}