简体   繁体   中英

how to select multiple values from dynamic dropdown in jquery

I'm trying to select multiple values from dynamic dropdown in jQuery. How can I achieve that any help that would be appreciated. Thanks In advance.

//jQuery

$.ajax({
    type: 'GET',
    url: url ,
    dataType: 'json',
    headers: {
      accept: "application/json",
      authorization:"key "+ bearer_token
    }
  }).done(function(data) {

  var options = '<option value="" disabled="" selected="">Select Nearest Airport</option>';
  data= data|| {};

   $( "select option:selected" ).each( data, function( key, value ) {
       options += '<option value="'+value.code+ ' - ' +value.name+'">'+value.code+ ' - ' +value.name+'</option>';
   })

    $('#AirportCode').html(options);
})

<div class="form-group col-md-6">
  <label for="airport">Nearest Airport:</label>
  <select id="AirportCode" name="AirportCode" multiple="multiple" class="form-control" placeholder="Select an Airport">
    <option value="" disabled selected>Select Nearest Airport</option>
    <option value="AirportCode" selected="selected"><div id="table"></div></option>
  </select>
</div>

Try change your each for this

$.each(data, function(key, value) {
    options += '<option value="' + value.code + ' - ' + value.name + '">' + value.code + ' - ' + value.name + '</option>';
  });

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