prokki/twbs-toggle-buttons 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

prokki/twbs-toggle-buttons

Composer 安装命令:

composer require prokki/twbs-toggle-buttons

包简介

A small javascript snippet to use Bootstrap Button Groups (https://getbootstrap.com/components/button-group/) for toggle buttons.

README 文档

README

License Latest Stable Version https://jquery.com/ https://getbootstrap.com/docs/4.3/

This is a small javascript snippet extending the Bootstrap Button Groups to use the toggle buttons as radio buttons or checkboxes.

Have a look at jsfiddle.

Table of Contents

Integrations

Download the code from GitHub and copy the dist directory to your project.

Include the following lines of code in the <head> section of your HTML.

<script src="path/to/jquery.twbs-toggle-buttons.min.js"></script>

The Basics

<label class="control-label">Is it true?</label>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
    <div class="btn active" role="button">
        <input type="radio" name="options" value="1" required="required">yes 1
    </div>
    <div class="btn active" role="button">
        <input type="radio" name="options" value="2">yes 2
    </div>
    <div class="btn" role="button">
        <input type="radio" name="options" value="0">no
    </div>
</div>
<script>
    $(".btn-group-toggle").twbsToggleButtons();
</script>

Options

twbsBtnSelector

default: '[role="button"]'

The default selector of all buttons is [role="button"]. To avoid this special html attribute you can change the option twbsBtnSelector, for instance to a class.

<label class="control-label">Is it true?</label>
<div class="btn-group btn-group-toggle">
    <div class="btn" required="required">yes</div>
    <div class="btn">no</div>
</div>
<script>
    $(".btn-group-toggle").twbsToggleButtons({
        twbsBtnSelector: ".btn"
    });
</script>

classActive

default: 'btn-success'

To detect an optical difference between active and inactive toggle buttons the two options classActive and classInactive are set to default bootstrap classes. Change the options and use other classes to make a difference between both toggle states.

Hint: Use an array of classes to set multiple classes.

$(".btn-group-toggle").twbsToggleButtons({
    classActive: "my-active-button",
    classInactive: ["my-inactive-button-1", "my-inactive-button-2"]
});

classInactive

default: 'btn-secondary'

See classActive

Events

To avoid problems with the order of executing click events on the twbs-toggle-buttons you can use two event types to handle events.

Click Event

The click event is fired on the button.

❗ Be careful to get the right state of the button! To get the state after button toggling use event twbsToggleButtons:activate Event.

$(".btn-group-toggle").find("[role='button']").on("click", function (e)
{
    console.log(e);
});

twbsToggleButtons:activate Event

This event is fired after the state of the button getting activated was changed.

$(".btn-group-toggle").find("[role='button']").on("twbsToggleButtons:activate", function (e)
{
    console.log(e);
});

Tricks and Tips

Checkboxes or Radio Buttons

<label class="control-label">Choose your documents!</label>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
    <div class="btn active" role="button">
        <input type="checkbox" name="options[]" value="1">Document 1
    </div>
    <div class="btn active" role="button">
        <input type="checkbox" name="options[]" value="2">Document 2
    </div>
    <div class="btn" role="button">
        <input type="checkbox" name="options[]" value="3">Document 3
    </div>
</div>

Required Radio Group

If radio buttons are used, usually exactly one button must be active.

To allow a button group without any active button, dismiss the attribute required in the input fields.

Overwrite classActive

Sometimes it is useful to change the active class of single toggle button. To do so, add the tml data attribute data-twbs-toggle-buttons-class-active with a corresponding class.

<label class="control-label">Is it true?</label>
<div class="btn-group btn-group-toggle" data-toggle="buttons">
    <div class="btn active" role="button">
        <input type="radio" name="options" value="1" required="required">yes
    </label>
    <div class="btn active" role="button">
        <input type="radio" name="options" value="0" data-twbs-toggle-buttons-class-active="btn-danger">no
    </label>
</div>

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 4
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固