2
我有一个简单的形式,一个简单的页面:如何在提交表单中使用jQuery和jQuery Mobile在序列化的字符串中给出重复?
<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;
})();
的解决方案,这将你为什么要使用`MAIN`的复杂的双重定义CONSOLE.LOG Object {name: "werwerwer", description: "erewwewerwerw"}
?它只会给你带来不必要的复杂情况。删除外部自我执行功能。它没有添加任何东西。另外,为什么你使用'new Main()'而不是'Main()'? – jfriend00 2013-02-15 01:43:24
在这里似乎很好http://jsfiddle.net/arunpjohny/crf7y/ – 2013-02-15 03:35:52
@ jfriend00,我使用'MAIN'作为其他的东西,作为一个类,这只是一个工作示例 – Patrioticcow 2013-02-15 05:36:25