说,我有这个:
var html = '<form><input type="text" value="v" name="nnn"></form>';
现在我想“反序列化”它,我的意思是获取一个对象/数组,键为nnn
,值为v
。所以,
{
nnn: 'v',
otherInputField: 'itsValue'
}
最佳答案
使用serializeArray
var html = '<form><input type="text" value="v" name="nnn"></form>';
$(html).serializeArray();
如果它绝对必须是一个纯对象,你可以将数组处理成一个对象,如
$.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 {
o[this.name] = this.value || '';
}
});
return o;
};
关于javascript - 如何解释 jQuery 中的表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213764/