如何使用引导程序4创建{3状态}复选框按钮

时间:2018-07-05 19:21:04

标签: javascript html bootstrap-4

我正在创建过滤器表单,我想过滤出属性为high = true或high = false的对象,或者忽略该属性。
到目前为止,我得到的只是“ on”值。
另外,在第三个值(空值或其他值)上,我希望样式也不同,例如禁用。
有人知道这样做的简单方法吗?

function readValue() {
  var highValue = $('#high').val();
  $('#result').html(highValue);
  if (highValue === null) {
    //don't filter
  } else {
    //filter
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<div class="btn-group-toggle" data-toggle="buttons">
  <label onclick="readValue()" class="btn btn-secondary active">
    <input id="high" type="checkbox" checked autocomplete="off"> High
  </label>
</div>
Result:<div id='result'></div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>

4 个答案:

答案 0 :(得分:1)

Similar question已被要求,有人建议使用:indeterminate伪类来表示NULL状态-忽略该过滤器属性。

What is :indeterminate?

Bootstrap 4自定义复选框仅在通过JavaScript手动设置时使用:indeterminate伪类 http://getbootstrap.com/docs/4.1/components/forms/#custom-forms

:indeterminate是相当新的东西,并不是所有的浏览器都能正确地渲染它,所以我只想采用更简单的方法:

  • filter属性旁边的复选框,代表我们是否需要按该属性进行过滤
  • 用于显示true/yesfalse/no状态的复选框或单选按钮。

页面加载

未选择过滤器,并且其可用值被隐藏。

enter image description here

已选择/启用过滤器

如果选择了过滤器,则会显示其可用值。

enter image description here

提交后

提交表单时,您始终可以在以下位置传递2个值:

  • 是否启用了过滤器属性,即$().is(":checked")
  • 过滤器属性值,即yes/no

提琴:http://jsfiddle.net/aq9Laaew/72593/

jQuery插件

可空复选框的插件:http://vanderlee.github.io/tristate/

答案 1 :(得分:0)

以这种方式尝试:此按钮组是带有标签的Bootstrap UI单选输入的一部分。我没有时间或兴趣来美化这些单选按钮。

<script type="text/javascript">
    function renderRadioOptions() {
        var radioGroupOpacity = ( $(this).val() == '' ) ? 0.5 : 1.0;
        $("div#filter_options_group > label.btn").css('opacity', radioGroupOpacity);
    }

    $(function() {
        $("div#filter_options_group > label.btn").css('opacity', 0.5);
        $("input[name='options']").on('change', renderRadioOptions );
    });

</script>

<div class="btn-group btn-group-toggle" data-toggle="buttons" id="filter_options_group">
    <label class="btn btn-secondary active">
        <input type="radio" name="options" value="" id="option1" autocomplete="off" checked> 
        <i class="glyphicon glyphicon-remove">
    </label>
    <label class="btn btn-secondary">
        <input type="radio" name="options" value="low" id="option2" autocomplete="off"> Low
    </label>
    <label class="btn btn-secondary">
        <input type="radio" name="options" value="high" id="option3" autocomplete="off"> High
    </label>
</div>

答案 2 :(得分:0)

我想出了一种方法,当一个按钮指示状态时,可以使用一个按钮来切换多个状态。这就是我一直在寻找的-大多数代码都是可以从Internet的不同部分找到并组合在一起的!
如果您有一个建议使它更短,更容易,请发表评论或直接对其进行编辑:)

var $radios = $('input[type="radio"][name="high"]');
$('#result').html($radios.filter(':checked').val());

$('#toggler').click(function() {
  var colorClasses = ["btn-danger", "btn-success", "btn-secondary"];
  var $checked = $radios.filter(':checked');
  var $next = $radios.eq($radios.index($checked) + 1);
  if (!$next.length) {
    $next = $radios.first();
  }
  $next.prop("checked", true);
  var newValue = $radios.filter(':checked').val();
  $(this)
    .removeClass(colorClasses.join(" "))
    .addClass(colorClasses[newValue]);
  $('#result').html(newValue);
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<div class="btn-group-toggle" data-toggle="buttons">
  <input type="radio" name="high" value="2" checked hidden>
  <input type="radio" name="high" value="1" hidden>
  <input type="radio" name="high" value="0" hidden>
  <button type="button" id="toggler" class="btn btn-secondary">High</button>
</div>
Result:
<div id='result'></div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>

答案 3 :(得分:0)

我喜欢 Cold_Class 的回答,但我需要一些更通用的东西。基于那个答案,我做了这个。

Javascript:

function onTogglerClick(el) {
    // Get the radios that are children of the parent div element
    let $radios = $(el).children('input[type="radio"]');

    // Use the radio button vals to determine the array, note that the values are also the same as the class names
    let valsClasses = $radios.map(function () {
        return $(this).val();
    }).toArray();

    // Advance to the next radio button
    let $checked = $radios.filter(':checked');
    let $next = $radios.eq($radios.index($checked) + 1);
    if ($next.val() === undefined) {
        $next = $radios.first();
    }
    $next.prop("checked", true);

    // change the class of the parent element
    let newValue = $radios.filter(':checked').val();
    $(el)
        .removeClass(valsClasses.join(" "))
        .addClass(newValue);
}

css

/* 单选框样式 */

.unchecked:before {
    font-family: "Font Awesome 5 Free";
    font-weight: 200;
    content: "\f0c8";
    width: 24px;
    height: 24px;
}


.checked:before {
    font-family: "Font Awesome 5 Free";
    font-weight: 200;
    content: "\f14a";
    width: 24px;
    height: 24px;
}

.musthave:before {
    font-family: "Font Awesome 5 Free";
    font-weight: 200;
    content: "\f0fe";
    width: 24px;
    height: 24px;
}

.mustnothave:before {
    font-family: "Font Awesome 5 Free";
    font-weight: 200;
    content: "\f146";
    width: 24px;
    height: 24px;
}

HTML:

<ul>
@foreach (var tag in Model.Tags)
{
<li>
    <div class="btn-group-toggle toggler unchecked" data-toggle="buttons" onclick="onTogglerClick(this)">
        <input type="radio" name="@tag" value="unchecked" checked hidden>
        <input type="radio" name="@tag" value="checked" hidden>
        <input type="radio" name="@tag" value="musthave" hidden>
        <input type="radio" name="@tag" value="mustnothave" hidden>
        <label>@tag </label>
    </div>
</li>
}