开发者

Jquery - Getting all values of a multi select list

开发者 https://www.devze.com 2022-12-11 03:06 出处:网络
I am trying to use JQuery to get all of the the values that are in a multiple select box. I currently have a picklist thing going on where users are selected in a select list box and then added to the

I am trying to use JQuery to get all of the the values that are in a multiple select box. I currently have a picklist thing going on where users are selected in a select list box and then added to the PickList box. I am trying to use JQuery to get a somewhat formatted list (formatted with spaces) so I can parse that list later on. I am able to get a real weird string that is not formatted by doing this

$.map($('#PickList'), function(e) { return $(e).text(); } );

but this is not formatted with a space " " after each value and the string looks like it has a ton of whitespace in the front of it or something开发者_Go百科. Anyone know a way to do this? Thanks


I would think that you would want to get just the values from the selected options.

var selected = $.map( $('#PickList option:selected'),
                      function(e) { return $(e).val(); } );


var selectedOptions = $.map($('#PickList :selected'),
       function(e) { return $(e).text(); } );
var str = selectedOptions.join(' ');

If you want all options, replace :selected with option.

Without it, you're only selecting on elelment, so it's the same as $('#PickList').text()


This plugin might help you, it adds fluent helpers for most form operations:

$("#PickList").selectedValues();

// Also available:
$("#PickList").isSelected("one", "two", "three");
$("#PickList").firstSelectedItem();
$("#PickList").lastSelectedItem();


Try this

var multipleValues = $("#multiple").val() || [];
0

精彩评论

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