如何创建具有复选框的选择框

时间:2016-03-22 06:10:06

标签: javascript html css html5 css3

我正在尝试实现一个Selectbox,在该选择框中有一些复选框,如下图所示。当我点击选择框时,它会打开复选框,该复选框存在于其他div中,我使用过z-index但它不起作用 看起来像这样

enter image description here

<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>
<div class="styled-select slate">
<select>
<option>Here is the first option</option>
<option>The second option</option>
<option>The third option</option>
</select>
</div>

这是CSS: -

 <style>
  .multiselect {
    width: 200px;
    height: 20px;
}
.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;
 }




 .styled-select {
    background: url(http://i62.tinypic.com/15xvbd5.png) no-repeat 96% 0;
    height: 29px;
    overflow: hidden;
    width: 240px;
    }

    .styled-select select {
    background: transparent;
    border: none;
    font-size: 14px;
    height: 29px;
    padding: 5px; /* If you add too much padding here, the options won't show in IE */
     width: 268px;
}
    .styled-select.slate 
{
     background: url(http://i62.tinypic.com/2e3ybe1.jpg) no-repeat right center;
     height: 34px;
      width: 240px;
     }

    .styled-select.slate select {
     border: 1px solid #ccc;
     font-size: 16px;
     height: 34px;
     width: 268px;
     }

2 个答案:

答案 0 :(得分:0)

@Sheraz,而不是修改您的代码只需使用以下工作代码进行更新:

<强> HTML

<dt>
<a href="#">
  <span class="hida">Select</span>    
  <p class="multiSel"></p>  
</a>
</dt>

 <dd>
    <div class="mutliSelect">
        <ul>
            <li>
                <input type="checkbox" value="Apple" />Apple</li>
            <li>
                <input type="checkbox" value="Blackberry" />Blackberry</li>
            <li>
                <input type="checkbox" value="HTC" />HTC</li>
            <li>
                <input type="checkbox" value="Sony Ericson" />Sony Ericson</li>
            <li>
                <input type="checkbox" value="Motorola" />Motorola</li>
            <li>
                <input type="checkbox" value="Nokia" />Nokia</li>
        </ul>
    </div>
</dd>
<button>Filter</button>
</dl>

<强> CSS

     body {
    font: normal 14px/100% "Andale Mono", AndaleMono, monospace;
    color: #fff;
    padding: 50px;
    width: 300px;
    margin: 0 auto;
    background-color: #374954;
    }  

   .dropdown {
    position: absolute;
    top:50%;
    transform: translateY(-50%);
    }

a {
  color: #fff;
}

.dropdown dd,
.dropdown dt {
  margin: 0px;
  padding: 0px;
}

.dropdown ul {
  margin: -1px 0 0 0;
}

.dropdown dd {
  position: relative;
}

.dropdown a,
.dropdown a:visited {
  color: #fff;
  text-decoration: none;
  outline: none;
  font-size: 12px;
}

.dropdown dt a {
  background-color: #4F6877;
  display: block;
  padding: 8px 20px 5px 10px;
  min-height: 25px;
  line-height: 24px;
  overflow: hidden;
  border: 0;
  width: 272px;
}

.dropdown dt a span,
.multiSel span {
  cursor: pointer;
  display: inline-block;
  padding: 0 3px 2px 0;
}

.dropdown dd ul {
  background-color: #4F6877;
  border: 0;
  color: #fff;
  display: none;
  left: 0px;
  padding: 2px 15px 2px 5px;
  position: absolute;
  top: 2px;
  width: 280px;
  list-style: none;
  height: 100px;
  overflow: auto;
}

.dropdown span.value {
  display: none;
}

.dropdown dd ul li a {
  padding: 5px;
  display: block;
}

.dropdown dd ul li a:hover {
  background-color: #fff;
}

button {
  background-color: #6BBE92;
  width: 302px;
  border: 0;
  padding: 10px 0;
  margin: 5px 0;
  text-align: center;
  color: #fff;
  font-weight: bold;
}

<强> JS

/*
    Dropdown with Multiple checkbox select with jQuery - May 27, 2013
    (c) 2013 @ElmahdiMahmoud
    license: http://www.opensource.org/licenses/mit-license.php
*/

$(".dropdown dt a").on('click', function() {
  $(".dropdown dd ul").slideToggle('fast');
});

$(".dropdown dd ul li a").on('click', function() {
  $(".dropdown dd ul").hide();
});

function getSelectedValue(id) {
  return $("#" + id).find("dt a span.value").html();
}

$(document).bind('click', function(e) {
  var $clicked = $(e.target);
  if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
});

$('.mutliSelect input[type="checkbox"]').on('click', function() {

  var title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
    title = $(this).val() + ",";

  if ($(this).is(':checked')) {
    var html = '<span title="' + title + '">' + title + '</span>';
    $('.multiSel').append(html);
    $(".hida").hide();
  } else {
    $('span[title="' + title + '"]').remove();
    var ret = $(".hida");
    $('.dropdown dt a').append(ret);

  }
});


http://codepen.io/elmahdim/pen/hlmri

希望有所帮助,祝你好运。

答案 1 :(得分:0)