我使用下面的代码来尝试向bar对象添加一个新属性:
$(document).ready(function(){
$('form').on("submit",preventDefault);
});
function preventDefault(e) {
e.preventDefault();
$url="contact/send";
bar = $('form').serialize();
bar["foo"]='xyz';
//bar.foo="123";
console.log(bar);
}然而,我似乎从来没有意识到bar.foo的价值。如果我在Chrome中查看控制台,我总是得到类似如下的结果:
name=Grahame&email=foo%40bar.com&message=Hello.为什么我得不到foo的值/属性?
发布于 2013-05-23 18:45:32
bar是序列化的表单数据。它是一个字符串而不是一个对象。
如果你想添加数据,你需要序列化数据:
bar += "&" + encodeURIComponent('foo') + "=" + encodeURIComponent('xyz');(如果知道数据中没有URL特殊字符,则可以跳过encode URI组件)。
https://stackoverflow.com/questions/16711845
复制相似问题