简体   繁体   English

添加活动类不会影响Twitter Bootstrap中的切换按钮

[英]Adding active class doesn't affect toggle button in Twitter Bootstrap

I have a form with multi checkbox which is required field that I turned into twitter bootstrap toggle buttons. 我有一个带有多个复选框的表单,这是必填字段,我已将其转换为Twitter Bootstrap切换按钮。

I am trying to activate a selection button with js, by adding 'active' class to it. 我试图通过向其添加“活动”类来激活带有js的选择按钮。 I am able to add the class but when saving the form I am getting an error that the field is required. 我可以添加类,但是在保存表单时出现错误,该字段为必填字段。

What am I missing? 我想念什么?

Here is the form. 这是表格。

<div id="reminder">
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-primary">
            <input type="checkbox" name="reminder" id="id_reminder_1" value="1" title="Choose at least one reminder option">Now</label>
        <label class="btn btn-primary">
            <input type="checkbox" name="reminder" id="id_reminder_2" value="30" title="Choose at least one reminder option">30m</label>
        <label class="btn btn-primary">
            <input type="checkbox" name="reminder" id="id_reminder_3" value="60" title="Choose at least one reminder option">1h</label>
        <label class="btn btn-primary active">
            <input type="checkbox" name="reminder" id="id_reminder_4" value="120"
            title="Choose at least one reminder option">2h</label>
        <label class="btn btn-primary">
            <input type="checkbox" name="reminder" id="id_reminder_5" value="1440"
            title="Choose at least one reminder option">1d</label>
    </div>
</div>

This is how I am adding the class: 这就是我添加课程的方式:

    $('#reminder label:eq(3)').addClass('active')

You're only adding the class, but you don't actually select it. 您仅添加了类,但实际上没有选择它。 Add the selected attribute and it will work: 添加selected属性,它将起作用:

$('#reminder label:eq(3)').addClass('active').attr('selected',true);

Here is a demo 这是一个演示

Add class open not active to open the dropdown (if thats what you are trying to do). 添加类openactive打开下拉(如果多数民众赞成你正在尝试做的)。 You have to add the class to <li class='dropdown open'> which hosts the <ul class='dropdown-menu'> 您必须将类添加到托管<ul class='dropdown-menu'> <li class='dropdown open'> <ul class='dropdown-menu'>

btn-primary is the css that will make your buttons blue. btn-primary是使按钮变为蓝色的CSS。 It looks like this is what you are using for inactive buttons because the way you have your code, all the buttons should be blue. 看起来这就是您用于非活动按钮的方式,因为您使用代码的方式,所有按钮都应为蓝色。 If you are trying to make the button grey because grey will be your active button, then you can use removeClass('btn-primary'). 如果您试图将按钮设为灰色,因为灰色将是您的活动按钮,则可以使用removeClass('btn-primary')。 This will turn it grey. 这将变成灰色。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当Bootstrap切换按钮类处于活动状态时,jQuery会执行某些操作 - jQuery do something when Bootstrap toggle button class is active 将“活动”添加到 html 按钮不应用 styles 中提到的:活动伪 class - Adding ' active' to to html button doesn't apply the styles mentioned in the :active psuedo class Twitter Bootstrap按钮的活动状态 - Twitter Bootstrap Button Active State Twitter Bootstrap:删除/切换类似复选框的按钮组的活动状态 - Twitter Bootstrap: Remove/Toggle the active state of checkbox-like button group 我正在尝试通过添加和删除已设置样式但无法正常工作的活动 class 来创建切换按钮 - I'm trying to create toggle button by adding and removing active class that I've already styled but it wasn't working Twitter Bootstrap 3导航栏切换按钮,带有不同的图标 - Twitter Bootstrap 3 navbar toggle button with different icon twitter bootstrap按钮切换不起作用 - twitter bootstrap button toggle does not work Twitter 引导程序崩溃:更改切换按钮的显示 - Twitter bootstrap collapse: change display of toggle button jQuery切换通过Twitter Bootstrap按钮类 - jQuery toggle through Twitter Bootstrap button classes Twitter引导程序:按钮样式在下拉菜单上不起作用 - Twitter bootstrap: button style doesn't work on dropdown menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM