Using jQuery, how do you simulate the serialization of a form to select with multiple options selected in a call to $ .ajax?

Below is my call to $ .ajax, how can I put the selected values ​​into multiple data sections?

$.ajax({
    type: "post",
    url: "http://myServer" ,
    dataType: "text",
    data: {
        'service' : 'myService',
        'program' : 'myProgram',
        'start' : start,
        'end' : end ,
        },
    success: function(request) {
      result.innerHTML = request ;
    }   // End success
  }); // End ajax method

EDIT I had to include that I understand how to cyclically select selected parameters using this code:

$('#userid option').each(function(i) {
 if (this.selected == true) {

but how do I put this in my data: section?

+5
source share
3 answers

how about using an array?

data: {
    ...
    'select' : ['value1', 'value2', 'value3'],
    ...
},

edit : ah sorry, here is the code, a few caveats:

'select' : $('#myselectbox').serializeArray(),

To work with serializeArray (), all form elements must have a name attribute. the value 'select'above will be an array of objects containing the name and values ​​of the selected elements.

'select' : [
    { 'name' : 'box', 'value' : 1 },
    { 'name' : 'box', 'value' : 2 }
],

:

<select multiple="true" name="box" id="myselectbox">
   <option value="1" name="option1" selected="selected">One</option>
   <option value="2" name="option2" selected="selected">Two</option>
   <option value="3" name="option3">Three</option>
</select>
+5

@Owen, .

id = "mySelect" multiple = "true"

    var mySelections = [];
    $('#mySelect option').each(function(i) {
        if (this.selected == true) {
            mySelections.push(this.value);
        }
    });


    $.ajax({
      type: "post",
      url: "http://myServer" ,
      dataType: "text",
      data: {
        'service' : 'myService',
        'program' : 'myProgram',
        'selected' : mySelections
        },
      success: function(request) {
        result.innerHTML = request ;
      }
    }); // End ajax method
+4

, SELECT [].
, jQuery serialize() .
SELECT, , infact:

<select name="a[]">
    <option value="five">5</option>
    <option value="six">6</option>
    <option value="seven">7</option>
</select>

serialize : a [] = 0 & a [] = 1 & a [] = 2 PHP :

[a] => Array
    (
        [0] => 0
        [1] => 1
        [2] => 2
    )

where real values ​​are lost.

+2
source

All Articles