Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append variable to be posted to ajax serialized data

Tags:

json

jquery

ajax

I have a form that I can post all the data in the ajax call like this:

data: $("#myform).serialize(),

But in the function, I set a variable to be be posted

That variable is var favorite = true; or var favorite = false;

It will post that variable if I do this:

var favorite = true;
var first_name = $("myform input[name=first_name]").val();
data: { favorite: favorite, first_name: first_name, etc ... }

But I was wondering if i could something like:

data: $("#myform).serialize + {favorite: favorite}

Its not a big deal if thats not possible, I am just clean up the function.

like image 386
Brad Avatar asked Oct 21 '25 13:10

Brad


1 Answers

The serialize() method return a parameter string, so you could just use string concatenation like

data: $("#myform").serialize() + '&favorite=' + favorite
like image 92
Arun P Johny Avatar answered Oct 24 '25 05:10

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!