Search code examples
jqueryajaxjquery-ui-multiselect

How to pass array of values from multiselect into jquery ajax call?


I'm using jQuery multiSelect and want to pass the selections to my update script.

Here's my ajax call

var newnotesecurity = $("#nt_newnotesecurity").serializeArray();

$.ajax({
  url: 'notes-savenewnote.php',
  method: 'POST',
  data: {
    cache: false,
    companyid: companyid,
    entitytype: entitytype,
    entityid: entityid,
    noteinitials: newnoteinitials,
    notetext: newnotetext,
    notetags: newnotetags,
    notesecurity: newnotesecurity
  },
  success: function() {
    // blah
  },
  error: function(){
    // blah
  }
});

nt_newnotesecurity is the multiselect object. Obviously serializeArray is not working for me. It's just my latest attempt to get this working.

I end up passing in this:

notesecurity[0][name]=nt_newnotesecurity&
notesecurity[0][value]=2&
notesecurity[1][name]=nt_newnotesecurity&
notesecurity[1][value]=14

but I'd like to pass in this

nt_newnotesecurity=2&nt_newnotesecurity=14

or I'd be just as content to pass in a single value that I can explode later on, like this

notesecurity=2-14

Solution

  • According to the documentation for the multiselect plugin, just calling val() should get you all the values

    var newnotesecurity = $("#nt_newnotesecurity").val();