简体   繁体   中英

why is multi-select values appearing as csv and not array?

here is my html:

<select id="list" name="list" size=10 multiple>
  <option value="xx">opt-1</option>
  <option value="yy">opt-2</option>
  <option value="zz">opt-3</option>
</select>

I am getting value of this as:

var list_v = $("#list").val();

submitting using the .post call as:

'&mylist='+list_v

in the cgi I am collecting like this(perl):

@list_values = $q->param('mylist');

I am getting all selected options as just one element in the @list_values array. I thought i should be getting all values as separate elements of the array!!!

So, for example, if anyone selects opt-1 and opt-2 I expect in @list_values [xx yy] (two values as two elements)

BUT, I am instead getting [xx,yy ] (two values as one element)

If I understood you correctly, you should make use of .serialize method. It would give you list[]=xx&list[]=yy if two options were selected. Then you no longer have to construct your data parameters string manually:

$.ajax({
    url: '/post-collector',
    method: 'POST',
    data: $('#your-form').serialize()
});

You can see the difference here: http://jsfiddle.net/QeqvF/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM