jquery .serializeArray(); add another value on top for passing to ajax

泪湿孤枕 提交于 2019-11-27 17:26:43

问题


im doing following

var data = $(form).serializeArray();
// now i want to  add another value on this data
data.username = 'this is username';

i want to know how can i add another value after doing serializeArray(), i tried all the things i know, but nothing is getting it to work. any ideas pls.


回答1:


try

data[data.length] = { name: "username", value: "The Username" };



回答2:


var data = $(form).serializeArray();
data.push({name: 'username', value: 'this is username'});

see also: jQuery post() with serialize and extra data




回答3:


var FormAttr = $('#form_id').serializeArray();

FormAttr.push({name: "Name_Of_Attribute", value:"Value_Of_Attributes"});



回答4:


Late to the party, but I personally prefer

const data = $(form).serializeArray().concat({
    name: "username", value: "The Username"
});



回答5:


I think it's just

data['username'] = 'this is a username';


来源:https://stackoverflow.com/questions/1726583/jquery-serializearray-add-another-value-on-top-for-passing-to-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!