简体   繁体   中英

how do you read the values of ALL items in a listbox using jquery

I have a listbox, and I want to read all of the items in a listbox using jquery. I also would like to be able to identify for each item if it is selected or not. What is the simplest way of doing this?

$('.myListBox option').each(function(index) {
  if ( ($(this).is(':selected')) {
    // do stuff if selected
  }
  else {
   // this one isn't selected, do other stuff
  }
});

Without any other information on how you want the output formatted:

$('#selectId').find('option').map(function() {
    return $(this).val() + ':' + $(this).is(':selected');
});

See example →

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