How to use Checkbox inside Select Option

前端 未结 12 2119
夕颜
夕颜 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:53

    Only add class create div and add class form-control. iam use JSP,boostrap4. Ignore c:foreach.

    <div class="multi-select form-control" style="height:107.292px;">
            <div class="checkbox" id="checkbox-expedientes">
                <c:forEach var="item" items="${postulantes}">
                    <label class="form-check-label">
                        <input id="options" class="postulantes" type="checkbox" value="1">Option 1</label>
                </c:forEach>
            </div>
        </div>
    
    0 讨论(0)
  • 2020-11-22 09:55

    You can try Bootstrap-select. It has a live search too!

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

    For a Pure CSS approach, you can use the :checked selector combined with the ::before selector to inline conditional content.

    Just add the class select-checkbox to your select element and include the following CSS:

    .select-checkbox option::before {
      content: "\2610";
      width: 1.3em;
      text-align: center;
      display: inline-block;
    }
    .select-checkbox option:checked::before {
      content: "\2611";
    }
    

    You can use plain old unicode characters (with an escaped hex encoding) like these:

    • ☐ Ballot Box - \2610
    • ☑ Ballot Box With Check - \2611

    Or if you want to spice things up, you can use these FontAwesome glyphs

    • .fa-square-o .fa-square-o - \f096
    • .fa-check-square-o .fa-check-square-o - \f046

    Demo in jsFiddle & Stack Snippets

    select {
      width: 150px;
    }
    
    .select-checkbox option::before {
      content: "\2610";
      width: 1.3em;
      text-align: center;
      display: inline-block;
    }
    .select-checkbox option:checked::before {
      content: "\2611";
    }
    
    .select-checkbox-fa option::before {
      font-family: FontAwesome;
      content: "\f096";
      width: 1.3em;
      display: inline-block;
      margin-left: 2px;
    }
    .select-checkbox-fa option:checked::before {
      content: "\f046";
    }
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
    
    <h3>Unicode</h3>
    <select multiple="" class="form-control select-checkbox" size="5">
      <option>Dog</option>
      <option>Cat</option>
      <option>Hippo</option>
      <option>Dinosaur</option>
      <option>Another Dog</option>
    </select>
    
    <h3>Font Awesome</h3>
    <select multiple="" class="form-control select-checkbox-fa" size="5">
      <option>Dog</option>
      <option>Cat</option>
      <option>Hippo</option>
      <option>Dinosaur</option>
      <option>Another Dog</option>
    </select>

    Note: Beware of IE compatibility issues however

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

    If you want to create multiple select dropdowns in the same page:

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

    Html:

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

    Using Jquery:

     function showCheckboxes(elethis) {
            if($(elethis).next('#checkboxes').is(':hidden')){
                $(elethis).next('#checkboxes').show();
                $('.selectBox').not(elethis).next('#checkboxes').hide();  
            }else{
                $(elethis).next('#checkboxes').hide();
                $('.selectBox').not(elethis).next('#checkboxes').hide();
            }
     }
    
    0 讨论(0)
  • 2020-11-22 10:01

    I started from @vitfo answer but I want to have <option> inside <select> instead of checkbox inputs so i put together all the answers to make this, there is my code, I hope it will help someone.

    $(".multiple_select").mousedown(function(e) {
        if (e.target.tagName == "OPTION") 
        {
          return; //don't close dropdown if i select option
        }
        $(this).toggleClass('multiple_select_active'); //close dropdown if click inside <select> box
    });
    $(".multiple_select").on('blur', function(e) {
        $(this).removeClass('multiple_select_active'); //close dropdown if click outside <select>
    });
    	
    $('.multiple_select option').mousedown(function(e) { //no ctrl to select multiple
        e.preventDefault(); 
        $(this).prop('selected', $(this).prop('selected') ? false : true); //set selected options on click
        $(this).parent().change(); //trigger change event
    });
    
    	
    	$("#myFilter").on('change', function() {
          var selected = $("#myFilter").val().toString(); //here I get all options and convert to string
          var document_style = document.documentElement.style;
          if(selected !== "")
            document_style.setProperty('--text', "'Selected: "+selected+"'");
          else
            document_style.setProperty('--text', "'Select values'");
    	});
    :root
    {
    	--text: "Select values";
    }
    .multiple_select
    {
    	height: 18px;
    	width: 90%;
    	overflow: hidden;
    	-webkit-appearance: menulist;
    	position: relative;
    }
    .multiple_select::before
    {
    	content: var(--text);
    	display: block;
      margin-left: 5px;
      margin-bottom: 2px;
    }
    .multiple_select_active
    {
    	overflow: visible !important;
    }
    .multiple_select option
    {
    	display: none;
        height: 18px;
    	background-color: white;
    }
    .multiple_select_active option
    {
    	display: block;
    }
    
    .multiple_select option::before {
      content: "\2610";
    }
    .multiple_select option:checked::before {
      content: "\2611";
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select id="myFilter" class="multiple_select" multiple>
      <option>A</option>
      <option>B</option>
      <option>C</option>
      <option>D</option>
      <option>E</option>
    </select>

    0 讨论(0)
  • 2020-11-22 10:03

    The best plugin so far is Bootstrap Multiselect

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    
    <title>jQuery Multi Select Dropdown with Checkboxes</title>
    
    <link rel="stylesheet" href="css/bootstrap-3.1.1.min.css" type="text/css" />
    <link rel="stylesheet" href="css/bootstrap-multiselect.css" type="text/css" />
    
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="js/bootstrap-3.1.1.min.js"></script>
    <script type="text/javascript" src="js/bootstrap-multiselect.js"></script>
    
    </head>
    
    <body>
    
    <form id="form1">
    
    <div style="padding:20px">
    
    <select id="chkveg" multiple="multiple">
    
        <option value="cheese">Cheese</option>
        <option value="tomatoes">Tomatoes</option>
        <option value="mozarella">Mozzarella</option>
        <option value="mushrooms">Mushrooms</option>
        <option value="pepperoni">Pepperoni</option>
        <option value="onions">Onions</option>
    
    </select>
    
    <br /><br />
    
    <input type="button" id="btnget" value="Get Selected Values" />
    
    <script type="text/javascript">
    
    $(function() {
    
        $('#chkveg').multiselect({
    
            includeSelectAllOption: true
        });
    
        $('#btnget').click(function(){
    
            alert($('#chkveg').val());
        });
    });
    
    </script>
    
    </div>
    
    </form>
    
    </body>
    </html>
    

    Here's the DEMO

    $(function() {
    
      $('#chkveg').multiselect({
        includeSelectAllOption: true
      });
    
      $('#btnget').click(function() {
        alert($('#chkveg').val());
      });
    });
    .multiselect-container>li>a>label {
      padding: 4px 20px 3px 20px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://davidstutz.de/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
    <link href="https://davidstutz.de/bootstrap-multiselect/docs/css/bootstrap-3.3.2.min.css" rel="stylesheet"/>
    <link href="https://davidstutz.de/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" rel="stylesheet"/>
    <script src="https://davidstutz.de/bootstrap-multiselect/docs/js/bootstrap-3.3.2.min.js"></script>
    
    <form id="form1">
      <div style="padding:20px">
    
        <select id="chkveg" multiple="multiple">
          <option value="cheese">Cheese</option>
          <option value="tomatoes">Tomatoes</option>
          <option value="mozarella">Mozzarella</option>
          <option value="mushrooms">Mushrooms</option>
          <option value="pepperoni">Pepperoni</option>
          <option value="onions">Onions</option>
        </select>
        
        <br /><br />
    
        <input type="button" id="btnget" value="Get Selected Values" />
      </div>
    </form>

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