0

I am submitting a form through ajax. Currently, I put together the post data like this:

for(i=0; i<document.formname.elements.length; i++){
  if(document.formname.elements[i].type=='checkbox' && document.formname.elements[i].checked!=true){continue;}
  if (i>0){data = data + "&";}
  data = data + document.formname.elements[i].name + "=" + encodeURIComponent(document.formname.elements[i].value);
}

This worked until I changed one of my select boxes to select multiple. This code snippet only gets the one of the selected values.

imagine I have this in my form:

<select name="selectme[]" multiple>
  <option value="">Select Something</option>
  <option value="987987">Choice 1</option>
  <option value="35987">Another Choice</option>
  <option value="98987">Choice 3</option>
</select>

How would I detect and gather the additional selections?

TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • possible duplicate of [getting all selected values of a multiple select box when clicking on a button using javascript?](http://stackoverflow.com/questions/5866169/getting-all-selected-values-of-a-multiple-select-box-when-clicking-on-a-button-u) – TecBrat Jan 22 '14 at 20:16
  • Yes, I flagged my own question. :) – TecBrat Jan 22 '14 at 20:17
  • glad I took the time to answer it then :/ – pennstatephil Jan 22 '14 at 21:31
  • @pennstatephil I appreciate your effort, but I was looking for the vanilla version. I kept digging and finally found a working function as an answer to the other question. – TecBrat Jan 22 '14 at 21:39

1 Answers1

0

Try using jquery's Serialize() on the form.

if you want to go through manually, look for the "selected" attribute on each option.

pennstatephil
  • 1,593
  • 3
  • 22
  • 43