开发者

Send list of :selected from multiple selectable select via getJSON

I have a list of dates in a multiple select elemental, and I need to send the list of selected dates to server for handling.

The jQuery is going ha开发者_如何学Goywire giving me a continuous increase in both memory and cpu usage, which I dont understand as running the haywire part alone is fine.

HTML
<select id="my-dates" name="my-dates[]" multiple="multiple" size="5">
  <option>2011-01-18</option>
  <option>2011-01-20</option>
  <option>2011-01-21</option>
  <option>2011-01-27</option>
</select>

jQuery
$.getJSON('dates_handling.php',{
 dates: $('select#my-dates').find(':selected'),
 other:stuff
},function(result){
  // result handling
});
data key 'dates' should contain an transmit-able array like ['2011-01-20',2011-01-27']

jQuery haywire part
$('select#my-dates').find(':selected')


I'd build the date array manually because you are passing full jQuery objects:

var selectedDates = [];

$('select#my-dates > option:selected').each(function() {
    selectedDates.push($(this).html());
});

$.getJSON('dates_handling.php',{
 dates: selectedDates,
 other:stuff
},function(result){
  // result handling
});


You can also use:

$('#my-dates').val()

It returns the values selected as an array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜