Frage

<select id="program" multiple="multiple">
    <option value="movie">Movie</option>
    <option value="series">TV Series</option>
    <option value="episode">Episode</option>
</select>

In the above select, I want to dynamically select option movie and series.

  var options = [movie,series]
  options.forEach(function(e){
       $("#program select").attr('value',e);
  });

Above code selects only series option.

War es hilfreich?

Lösung

Set the selected property on the options, rather than setting the value of the select:

$("#program option[value=" + e + "]").prop('selected', true);

Here's a fiddle

Andere Tipps

billyonecan's answer is correct. If you want more advanced multiselect, then see here. Demos here.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top