参见英文答案 >
Convert form data to JavaScript object with jQuery 51个
我试图将HTML表单数据转换为JSON对象,我有
this thread,但我不知道为什么它不适合我.我使用以下代码.
Title
Your Name
Contact No:
Description:
Email:
City:
Country:
Picture 1:
Picture 2:
Picture 3:
Picture 4:
Picture 5:
Your Demand:
$(document).ready(function(){
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] === undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
alert(this.name);
o[this.name] = this.value || '';
}
});
return o;
};
$('#myform').submit(function() {
$('#result').text(JSON.stringify($('#myform').serializeObject()));
return false;
});
});
我试着调试它,我注意到当我的函数运行时,它总是在else语句中运行代码.