RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 607383
Accepted
Fox
Fox
Asked:2020-12-24 22:22:06 +0000 UTC2020-12-24 22:22:06 +0000 UTC 2020-12-24 22:22:06 +0000 UTC

有必要用按钮重置bootstrap radiobutton的两个值

  • 772

告诉我如何使用链接中此代码中的按钮重置这两个值 -

http://jsfiddle.net/9o41oykp/34/

$(function () {
    $('.button-checkbox').each(function () {

        // Settings
        var $widget = $(this),
            $button = $widget.find('button'),
            $checkbox = $widget.find('input:radio'),
            color = $button.data('color'),
            settings = {
                on: {
                    icon: 'glyphicon glyphicon-check'
                },
                off: {
                    icon: 'glyphicon glyphicon-unchecked'
                }
            };

        // Event Handlers
        $button.on('click', function () {
            $checkbox.prop('checked', !$checkbox.is(':checked'));
            $checkbox.triggerHandler('change');
            updateDisplay();
        });

        // Actions
        function updateDisplay() {
            $('.button-checkbox').each(function () {
                var isChecked = $(this).find('input:radio').is(':checked');
                // Set the button's state
                $(this).find('button').data('state', (isChecked) ? "on" : "off");
                // Set the button's icon
                $(this).find('button').find('.state-icon')
                    .removeClass()
                    .addClass('state-icon ' + settings[$(this).find('button').data('state')].icon);
                // Update the button's color
                if (isChecked) {
                    $(this).find('button')
                        .removeClass('btn-default')
                        .addClass('btn-' + color + ' active');
                } else {
                    $(this).find('button')
                        .removeClass('btn-' + color + ' active')
                        .addClass('btn-default');
                }
            })
        }
        // Initialization
        function init() {
            updateDisplay();
            // Inject the icon if applicable
            if ($button.find('.state-icon').length == 0) {
                $button.prepend('<i class="state-icon ' + settings[$button.data('state')].icon + '"></i> ');
            }
        }
        init();
    });
});
$('#reset').click(function() {
        $('.button-checkbox').find('.glyphicon').toggleClass('glyphicon-check glyphicon-unchecked');
});
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">Option 1</button>
        <input type="radio" class="xhidden" name="group1" id="op1" checked />
</span>
<span class="button-checkbox">
        <button type="button" class="btn btn-lg" data-color="primary">Option 2</button>
        <input type="radio" class="xhidden" name="group1" id="op2"  />
</span>

<br><br><button type="button" class="btn btn-info" id="reset">Reset<span class="glyphicon glyphicon-send"></span></button>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

jquery
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Gleb Kemarsky
    2020-12-25T02:20:55Z2020-12-25T02:20:55Z

    要从Reset图标中删除复选标记并通过单击使所有按钮变灰:

    1. 找到类的图标.glyphicon-check;
    2. 我们剥夺了父母的颜色;
    3. 图标本身被替换为没有复选标记的图标。

    $('#reset').click(function() {
        /* 1. */
        var $icon = $('.button-checkbox .glyphicon-check');
    
        /* 2. */
        var $button = $icon.parent();
        $button.removeClass('btn-' + $button.data('color') + ' active').addClass('btn-default');
    
        /* 3. */
        $icon.removeClass('glyphicon-check').addClass('glyphicon-unchecked');
    });
    
    $(function () {
        $('.button-checkbox').each(function () {
    
            // Settings
            var $widget = $(this),
                $button = $widget.find('button'),
                $checkbox = $widget.find('input:radio'),
                color = $button.data('color'),
                settings = {
                    on: {
                        icon: 'glyphicon glyphicon-check'
                    },
                    off: {
                        icon: 'glyphicon glyphicon-unchecked'
                    }
                };
    
            // Event Handlers
            $button.on('click', function () {
                $checkbox.prop('checked', !$checkbox.is(':checked'));
                $checkbox.triggerHandler('change');
                updateDisplay();
            });
    
            // Actions
            function updateDisplay() {
                $('.button-checkbox').each(function () {
                    var isChecked = $(this).find('input:radio').is(':checked');
                    // Set the button's state
                    $(this).find('button').data('state', (isChecked) ? "on" : "off");
                    // Set the button's icon
                    $(this).find('button').find('.state-icon')
                        .removeClass()
                        .addClass('state-icon ' + settings[$(this).find('button').data('state')].icon);
                    // Update the button's color
                    if (isChecked) {
                        $(this).find('button')
                            .removeClass('btn-default')
                            .addClass('btn-' + color + ' active');
                    } else {
                        $(this).find('button')
                            .removeClass('btn-' + color + ' active')
                            .addClass('btn-default');
                    }
                })
            }
            // Initialization
            function init() {
                updateDisplay();
                // Inject the icon if applicable
                if ($button.find('.state-icon').length == 0) {
                    $button.prepend('<i class="state-icon ' + settings[$button.data('state')].icon + '"></i> ');
                }
            }
            init();
        });
    });
    <span class="button-checkbox">
            <button type="button" class="btn btn-lg" data-color="primary">Option 1</button>
            <input type="radio" class="xhidden" name="group1" id="op1" checked />
    </span>
    <span class="button-checkbox">
            <button type="button" class="btn btn-lg" data-color="primary">Option 2</button>
            <input type="radio" class="xhidden" name="group1" id="op2"  />
    </span>
    
    <br><br><button type="button" class="btn btn-info" id="reset">Reset<span class="glyphicon glyphicon-send"></span></button>
    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5