jquery - 重置 Bootstrap 单选按钮

标签 jquery twitter-bootstrap twitter-bootstrap-3

这一定是一个重复的问题,但我找不到答案。如何重置 Bootstrap 单选按钮?

根据文档,它已重置,但它没有看到执行任何操作,我也尝试使用 jquery 来更改它,但什么也没做。

$('#Reset').click(function(){
$('#option1').button('reset');
  $("#option1").prop('checked', true);
});

这是 fiddle 。 https://jsfiddle.net/m1jryanh/我点击另一个按钮,然后点击重置按钮,希望它会重置,但事实并非如此。

最佳答案

取消选择所有选项

如果您指的是更新 UI,您可以从所有按钮(或您的案例标签)本身中显式删除 active 类:

$('#Reset').click(function(){
   // Explicitly uncheck all of your radio buttons
   $('[data-toggle="buttons"] :radio').prop('checked', false);
   // Select all label elements within your toggle and remove the active class
   $('[data-toggle="buttons"] label').removeClass('active');
});

您可以see this in action here并演示如下:

enter image description here

重置为第一个选项

同样,如果您想将其设置为初始选项,您只需触发第一个元素的 click 事件即可:

$('#Reset').click(function(){
   // Explicitly uncheck all of your radio buttons
   $('[data-toggle="buttons"] :radio').prop('checked', false);
   // Select all label elements within your toggle and remove the active class
   $('[data-toggle="buttons"] label').removeClass('active');

   // Set the first one as checked and selected
   $('[data-toggle="buttons"] :radio:first').addClass('active');
   // Do the same thing for the active indicator
   $('[data-toggle="buttons"] label:first').addClass('active');
});

您可以see this option in action here并演示如下:

enter image description here

关于jquery - 重置 Bootstrap 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36800463/

相关文章:

javascript - JQuery Tablesorter 不会对 session 脚本加载的 MySQL PHP 表进行排序

jquery - 使用外部按钮切换饼图数据

javascript - 将多个 Bootstrap 菜单合并为一个用于移动设备?

css - 如何在 Bootstrap 中实现图表?

jquery - 使用 JQuery 从 JSON 或 XML 提要生成 HTML 表单

javascript - 仅当验证检查返回 true 时才通过 ajax 进行 POST

css - 其他输入旁边的 Bootstrap 文本字段,使用整个宽度

html - 使容器 div 用粘性页脚填充所有布局

forms - Bootstrap - 为什么 "control-label"类不影响标签?

css - 试图找出本网站 slider 的 @media 查询 css 内容