在选择下拉列表中选择多个选项

问题描述 投票:0回答:3

如何在此国家/地区下拉列表中选择多个值

这是我的代码:

function print_country(country_id) {
  // given the id of the <select> tag as function argument, it inserts <option> tags
  var option_str = document.getElementById(country_id);
  option_str.length = 0;
  option_str.options[0] = new Option('Select Country', '');
  option_str.selectedIndex = 0;
  for (var i = 0; i < country_arr.length; i++) {
    option_str.options[option_str.length] = new Option(country_arr[i], country_arr[i]);
  }
}
javascript select option
3个回答
1
投票

使用option.selected属性,如下所示:

var option_str = document.getElementById('country_id');
for ( var i = 0; i < option_str.options.length; i++ )
{
  if ( //condition for selecting the current option)
  {
    option_str.options[i].selected = true;
  }
}

0
投票

您可以将multiple属性添加到现有选择中。它允许您使用CTRL按钮选择多个值。

<select multiple></select>

工作范例here


0
投票
<select class="form-control select2 color-gray" multiple="multiple">
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
    <option>Option 5</option>
</select>

jQuery的

$(".select2").select2();

你可以在github上下载select2插件。链接提到下面。 https://github.com/select2/select2/tree/develop/dist/js

© www.soinside.com 2019 - 2024. All rights reserved.