html把表单的值转成json对象,jquery – 将表单数据转换为JSON对象

参见英文答案 >

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:

hello

$(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语句中运行代码.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值