php - 通过 jQuery 取消选中 Bootstrap 单选按钮

标签 php jquery html css twitter-bootstrap

我在使用 Bootstrap 插件时遇到问题。 我在以下代码中有一些单选按钮:

    <div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary active">
    <input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
  </label>
  <label class="btn btn-primary">
    <input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
  </label>
  <label class="btn btn-primary">
    <input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
  </label>
   </div>

我想放一个按钮来重置选择。我试过重置按钮 ( <button type="reset"> )、jQuery 命令,但都失败了。

在 Bootstrap API 中我找到了一个解释: http://getbootstrap.com/javascript/#buttons-checkbox-radio

说的是:

Visual checked state only updated on click If the checked state of a checkbox button is updated without firing a click event on the button (e.g. via or via setting the checked property of the input), you will need to toggle the .active class on the input's label yourself.

有人可以帮我找到解决问题的办法吗?

谢谢, 加布里埃尔。

最佳答案

首先,您需要从 label 标签中移除 active 类,然后使 radio 元素上的 properties checked 元素变为 false 状态,试试下面这个简单的例子:

$('button').click(function () {
  $('.btn-group').find('label').removeClass('active')
  .end().find('[type="radio"]').prop('checked', false);
});

DEMO

关于php - 通过 jQuery 取消选中 Bootstrap 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32262205/

相关文章:

php - Codeigniter 3 fatal error : Call to a member function database() on null

php - 连接 html、javascript、php 和 mysql

php - 使用 preg_replace 更改字符串名称删除 espace 重音和点,但允许点扩展

javascript - 如何构建具有多个搜索字段的搜索页面?

php - 从 Woocommerce 中的特定产品类别中删除 "No products found"

javascript - 如何使用 angularjs 开发 firefox 插件

javascript - 将包含应用于 jQuery UI 可排序表可防止将高行移动到最后一个位置

javascript - 如何设置按钮元素以便可以使用箭头键控制它们?

html - CSS 高度属性和嵌套列表

html - 在 h :panelGrid 中对齐一个单元格