是否可以将输入复选框设为Bootstrap glyphicon?

时间:2015-01-28 16:00:47

标签: css twitter-bootstrap checkbox

将输入复选框设为Bootstrap glyphicon是否可行?

我想用一个漂亮的Bootstrap glyphicon来组成默认复选框。 例如:glyphicon glyphicon-unchecked,选中时为glyphicon glyphicon-check

我试过这个:

input[type='checkbox'] {
  position: relative;
  top: 1px;
  display: inline-block;
  font-family: 'Glyphicons Halflings';
  font-style: normal;
  font-weight: 400;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  content: "\e157";
}

但什么都没发生..

我不知道这是否可行?

3 个答案:

答案 0 :(得分:7)

您可以通过以下几种方法实现这一目标:

方法1

  • <label>标记内,两个<tags>代表您需要放置的图标(或在每个使用场景之外)
  • 然后在选中或取消选中<tags>时切换这两个input[type='checkbox']
  • 进行。

方法2

  • 上述方法的更简洁方法是使用bootstraps图标中的css,并将:before(或:after取决于您的情节)放在<label>上标签
  • 然后切换content道具。 :before类,当您选中或取消选中input[type='checkbox']时,您想要的图标
  • 进行。

查看 demo here ,还有一些关于此问题的文档:

答案 1 :(得分:4)

如果您能稍微修改一下标记,则应该这样做:

<label for="myCheckbox" class="glyphy">
    <input type="checkbox" id="myCheckbox" /> 
    <span class="glyphicon glyphicon-unchecked"></span>
    label words
</label>

$('.glyphy').click(function (e) {
    if ($(e.target).is('input')) { // prevent double-event due to bubbling
        $(this).find('.glyphicon').toggleClass('glyphicon-check glyphicon-unchecked');
    }
});

Demo

答案 2 :(得分:1)

如果您有图标,可以将其设置为样式:http://jsfiddle.net/swm53ran/164/

/*hide checkbox and radio buttons*/
input[type=checkbox], 
input[type=radio] {
    width: 2em;
    margin: 0;
    padding: 0;
    font-size: 1em;
    opacity: 0; /*This is the part tht actually hides it*/
}

/*normalize the spacing*/
input[type=checkbox] + label,
input[type=radio] + label {
    display: inline-block;
    margin-left: -2em;
    line-height: 1.5em;
}

/*unchecked css*/
input[type=checkbox] + label > span,
input[type=radio] + label > span {
    display: inline-block;
    background-image: url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Face-sad.svg/48px-Face-sad.svg.png');
    width: 50px;
    height: 50px;
}

/*selected checkbox css*/
input[type=checkbox]:checked + label > span > span {
    width: 50px;
    height: 50px;
    display:block;
    background-image: url('http://wscont1.apps.microsoft.com/winstore/1x/a14c3995-34d7-454c-82e2-0c192e48b91a/Icon.173718.png');
}

/*selected radio css*/
input[type=radio]:checked + label > span > span {
    width: 50px;
    height: 50px;
    display:block;
    background-image: url('http://wscont1.apps.microsoft.com/winstore/1x/a14c3995-34d7-454c-82e2-0c192e48b91a/Icon.173718.png');
}

<div>
    <input id="check1" type="checkbox" name="check1" value="check1" />
    <label for="check1"><span><span></span></span>Checkbox</label>
</div>
<div>
    <input id="radio1" type="radio" name="radio" value="radio1" />
    <label for="radio1"><span><span></span></span>Radio1</label>
</div>
<div>
    <input id="radio2" type="radio" name="radio" value="radio2" />
    <label for="radio2"><span><span></span></span>Radio2</label>
</div>