How to use Checkbox inside Select Option

前端 未结 12 2118
夕颜
夕颜 2020-11-22 09:15

The client has given me a design which has a Select Option menu containing a checkbox together with the item name as individual items in the list. Is there anyway possible t

相关标签:
12条回答
  • 2020-11-22 09:38

    Alternate Vanilla JS version with click outside to hide checkboxes:

    let expanded = false;
    const multiSelect = document.querySelector('.multiselect');
    
    multiSelect.addEventListener('click', function(e) {
      const checkboxes = document.getElementById("checkboxes");
        if (!expanded) {
        checkboxes.style.display = "block";
        expanded = true;
      } else {
        checkboxes.style.display = "none";
        expanded = false;
      }
      e.stopPropagation();
    }, true)
    
    document.addEventListener('click', function(e){
      if (expanded) {
        checkboxes.style.display = "none";
        expanded = false;
      }
    }, false)
    

    I'm using addEventListener instead of onClick in order to take advantage of the capture/bubbling phase options along with stopPropagation(). You can read more about the capture/bubbling here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

    The rest of the code matches vitfo's original answer (but no need for onclick() in the html). A couple of people have requested this functionality sans jQuery.

    Here's codepen example https://codepen.io/davidysoards/pen/QXYYYa?editors=1010

    0 讨论(0)
  • 2020-11-22 09:39

    You can use this library on git for this purpose https://github.com/ehynds/jquery-ui-multiselect-widget

    for initiating the selectbox use this

        $("#selectBoxId").multiselect().multiselectfilter();
    

    and when you have the data ready in json (from ajax or any method), first parse the data & then assign the js array to it

        var js_arr = $.parseJSON(/*data from ajax*/);
        $("#selectBoxId").val(js_arr);
        $("#selectBoxId").multiselect("refresh");
    
    0 讨论(0)
  • 2020-11-22 09:51

    You might be loading multiselect.js file before the option list updated with AJAX call so while execution of multiselect.js file there is empty option list is there to apply multiselect functionlaity. So first update the option list by AJAX call then initiate the multiselect call you will get the dropdown list with the dynamic option list.

    Hope this will help you out.

    Multiselect dropdown list and related js & css files

    // This function should be called while loading page
    var loadParentTaskList = function(){
        $.ajax({
            url: yoururl,
            method: 'POST',
            success: function(data){
                // To add options list coming from AJAX call multiselect
                for (var field in data) {
                    $('<option value = "'+ data[field].name +'">' + data[field].name + '</option>').appendTo('#parent_task');
                }
       
                // To initiate the multiselect call 
                $("#parent_task").multiselect({
                    includeSelectAllOption: true
                })
            }
        });
    }
    // Multiselect drop down list with id parent_task
    <select id="parent_task" multiple="multiple">
    </select>

    0 讨论(0)
  • 2020-11-22 09:52

    You cannot place checkbox inside select element but you can get the same functionality by using HTML, CSS and JavaScript. Here is a possible working solution. The explanation follows.

    enter image description here


    Code:

    var expanded = false;
    
    function showCheckboxes() {
      var checkboxes = document.getElementById("checkboxes");
      if (!expanded) {
        checkboxes.style.display = "block";
        expanded = true;
      } else {
        checkboxes.style.display = "none";
        expanded = false;
      }
    }
    .multiselect {
      width: 200px;
    }
    
    .selectBox {
      position: relative;
    }
    
    .selectBox select {
      width: 100%;
      font-weight: bold;
    }
    
    .overSelect {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
    }
    
    #checkboxes {
      display: none;
      border: 1px #dadada solid;
    }
    
    #checkboxes label {
      display: block;
    }
    
    #checkboxes label:hover {
      background-color: #1e90ff;
    }
    <form>
      <div class="multiselect">
        <div class="selectBox" onclick="showCheckboxes()">
          <select>
            <option>Select an option</option>
          </select>
          <div class="overSelect"></div>
        </div>
        <div id="checkboxes">
          <label for="one">
            <input type="checkbox" id="one" />First checkbox</label>
          <label for="two">
            <input type="checkbox" id="two" />Second checkbox</label>
          <label for="three">
            <input type="checkbox" id="three" />Third checkbox</label>
        </div>
      </div>
    </form>

    Explanation:

    At first we create a select element that shows text "Select an option", and empty element that covers (overlaps) the select element (<div class="overSelect">). We do not want the user to click on the select element - it would show an empty options. To overlap the element with other element we use CSS position property with value relative | absolute.

    To add the functionality we specify a JavaScript function that is called when the user clicks on the div that contains our select element (<div class="selectBox" onclick="showCheckboxes()">).

    We also create div that contains our checkboxes and style it using CSS. The above mentioned JavaScript function just changes <div id="checkboxes"> value of CSS display property from "none" to "block" and vice versa.

    The solution was tested in the following browsers: Internet Explorer 10, Firefox 34, Chrome 39. The browser needs to have JavaScript enabled.


    More information:

    CSS positioning

    How to overlay one div over another div

    http://www.w3schools.com/css/css_positioning.asp

    CSS display property

    http://www.w3schools.com/cssref/pr_class_display.asp

    0 讨论(0)
  • 2020-11-22 09:52

    Try multiple-select, especially multiple-items. Looks to be much clean and managed solution, with tons of examples. You can also view the source.

    <div>
      <div class="form-group row">
        <label class="col-sm-2">
          Basic Select
        </label>
    
        <div class="col-sm-10">
          <select multiple="multiple">
            <option value="1">1</option>
            <option value="2">2</option>
          </select>
        </div>
      </div>
    
      <div class="form-group row">
        <label class="col-sm-2">
          Group Select
        </label>
    
        <div class="col-sm-10">
          <select multiple="multiple">
            <optgroup label="Group 1">
              <option value="1">1</option>
              <option value="2">2</option>
            </optgroup>
            <optgroup label="Group 2">
              <option value="6">6</option>
              <option value="7">7</option>
            </optgroup>
            <optgroup label="Group 3">
              <option value="11">11</option>
              <option value="12">12</option>
            </optgroup>
          </select>
        </div>
      </div>
    </div>
    
    <script>
      $(function() {
        $('select').multipleSelect({
          multiple: true,
          multipleWidth: 60
        })
      })
    </script>
    
    0 讨论(0)
  • 2020-11-22 09:52

    Use this code for checkbox list on option menu.

    .dropdown-menu input {
       margin-right: 10px;
    }   
    <div class="btn-group">
        <a href="#" class="btn btn-primary"><i class="fa fa-cogs"></i></a>
        <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
          <span class="caret"></span>
        </a>
        <ul class="dropdown-menu" style="padding: 10px" id="myDiv">
            <li><p><input type="checkbox" value="id1" > OA Number</p></li>
            <li><p><input type="checkbox" value="id2" >Customer</p></li>
            <li><p><input type="checkbox" value="id3" > OA Date</p></li>
            <li><p><input type="checkbox" value="id4" >Product Code</p></li>
            <li><p><input type="checkbox" value="id5" >Name</p></li>
            <li><p><input type="checkbox" value="id6" >WI Number</p></li>
            <li><p><input type="checkbox" value="id7" >WI QTY</p></li>
            <li><p><input type="checkbox" value="id8" >Production QTY</p></li>
            <li><p><input type="checkbox" value="id9" >PD Sr.No (from-to)</p></li>
            <li><p><input type="checkbox" value="id10" > Production Date</p></li>
            <button class="btn btn-info" onClick="showTable();">Go</button>
        </ul>
    </div>

    0 讨论(0)
提交回复
热议问题