<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.

有帮助吗?

解决方案

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top