点击下拉菜单多选

时间:2018-01-18 19:50:19

标签: javascript jquery html css twitter-bootstrap

我正在试图弄清楚如何让下拉菜单功能有多种选择。

目前我有类似的东西......

<div class="btn-group filter-buttons" id="fDropdown">
   <button data-toggle="dropdown" class="btn btn-default btn-block dropdown-toggle">Events
       <span class="caret"></span>
   </button>
   <ul class="dropdown-menu">
       <li><input type="radio" id="live1" name="feature" value="1" checked><label for="live1">1</label></li>
       <li><input type="radio" id="live2" name="feature" value="2"><label for="live2">2</label></li>
       <li><input type="radio" id="live3" name="feature" value="3"><label for="live3">3</label></li>
   </ul>
</div>

<script type="text/javascript">

    $(document).on('click', '#fDropdown', function (e) {
        e.stopPropagation();
    });

    (function($){
         var feature = '{{feature}}'; 
         //here feature is the value from the <li> chosen in the dropdown menu
         $('#fDropdown input').each(function(i,v){
            if ($(v).val() == feature){
                $(v).attr('checked', true);
                $(v).change();
            }
        });
    });

</script>

目前,它只能选择一个值。我不确定如何选择多个而不用 ctrl 点击或拖动。

1 个答案:

答案 0 :(得分:1)

将单选按钮更改为复选框:

<li>
  <input class="form-check-input" type="checkbox" value="" id="live1">
  <label class="form-check-label" for="live1">
    1
  </label>
</li>