jQuery $.ajax POST Multiple Select Selections

Mike George picture Mike George · Jul 17, 2012 · Viewed 12.3k times · Source

I have a multipe select as part of my imput form and I would like to submit it's selected values via jQuery AJAX.

I've tried to use the following syntax.

$('#multipleselect').val().join('|');

That works, bu the problem I am having is that the user is not required to select anything from the multiple select. If they don't I get an error because you cannot join a null object.

Any Suggestions?

Also keep in mind that there are other values being submitted. For example:

data: "name=" + $("#name").val() + "&multipleselect=" + $("#multipleselect").val().join('|');

Answer

undefined picture undefined · Jul 17, 2012

You can use serialize method:

Encode a set of form elements as a string for submission.

var data = $('form').serialize()