Bootstrap:如果选中两次,则不提交单选按钮值

时间:2013-11-19 20:20:24

标签: html twitter-bootstrap twitter-bootstrap-3

在我的表格中,我有一组单选按钮:

 <div class="btn-group" data-toggle="buttons">
    <label class="btn btn-danger">
        <input name="Status" type="radio" value="2">Failed</label>
    <label class="btn btn-warning">
        <input name="Status" type="radio" value="0">Pending</label>
    <label class="btn btn-success active">
        <input name="Status" type="radio" value="1">Success</label>
 </div>

当我选择一个无线电选项并提交表单时,一切都按预期工作,但如果我双击一个选项,该选项会被破坏,并且不会与表单提交一起发送,除非我检查另一个选项。

看起来data-toggle="buttons"属性会导致问题。

这是Demo

4 个答案:

答案 0 :(得分:1)

刚发现这是一个已知错误:https://github.com/twbs/bootstrap/issues/9920

答案 1 :(得分:1)

我在Github问题页面中看到了这个解决方案。这对我有用。

临时解决方案:

    $("input[type=radio][name=gender]").on("change", function(){
         $(this).prop('checked', true);
    });

答案 2 :(得分:0)

看起来这是v3.0中的一个已知错误,将在下一个版本中修复

Radio checked property is removed on second click with data-toggle="buttons

答案 3 :(得分:0)

我搜索了很多,发现这是一个bug,但现在已经解决了。参考https://github.com/twbs/bootstrap/issues/9920

已解决v3.0.2中的问题,请确定是否每晚构建或正式发布[Github]。 您可以使用特定的bootstrap js文件作为按钮,而不是使用bootstrap.min.js。 https://github.com/twbs/bootstrap/blob/5d6e9212a642430719702ead322103e19c6aa782/js/button.js

尝试使用bootstrap.button.js,文件内容如下:

/* ========================================================================
 * Bootstrap: button.js v3.0.2
 * http://getbootstrap.com/javascript/#buttons
 * ========================================================================
 * Copyright 2013 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ======================================================================== */


+function ($) { "use strict";

  // BUTTON PUBLIC CLASS DEFINITION
  // ==============================

  var Button = function (element, options) {
    this.$element = $(element)
    this.options  = $.extend({}, Button.DEFAULTS, options)
  }

  Button.DEFAULTS = {
    loadingText: 'loading...'
  }

  Button.prototype.setState = function (state) {
    var d    = 'disabled'
    var $el  = this.$element
    var val  = $el.is('input') ? 'val' : 'html'
    var data = $el.data()

    state = state + 'Text'

    if (!data.resetText) $el.data('resetText', $el[val]())

    $el[val](data[state] || this.options[state])

    // push to event loop to allow forms to submit
    setTimeout(function () {
      state == 'loadingText' ?
        $el.addClass(d).attr(d, d) :
        $el.removeClass(d).removeAttr(d);
    }, 0)
  }

  Button.prototype.toggle = function () {
    var $parent = this.$element.closest('[data-toggle="buttons"]')
    var changed = true

    if ($parent.length) {
      var $input = this.$element.find('input')
      if ($input.prop('type') === 'radio') {
        // see if clicking on current one
        if ($input.prop('checked') && this.$element.hasClass('active'))
          changed = false
        else
          $parent.find('.active').removeClass('active')
      }
      if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
    }

    if (changed) this.$element.toggleClass('active')
  }


  // BUTTON PLUGIN DEFINITION
  // ========================

  var old = $.fn.button

  $.fn.button = function (option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.button')
      var options = typeof option == 'object' && option

      if (!data) $this.data('bs.button', (data = new Button(this, options)))

      if (option == 'toggle') data.toggle()
      else if (option) data.setState(option)
    })
  }

  $.fn.button.Constructor = Button


  // BUTTON NO CONFLICT
  // ==================

  $.fn.button.noConflict = function () {
    $.fn.button = old
    return this
  }


  // BUTTON DATA-API
  // ===============

  $(document).on('click.bs.button.data-api', '[data-toggle^=button]', function (e) {
    var $btn = $(e.target)
    if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
    $btn.button('toggle')
    e.preventDefault()
  })

}(jQuery);

查看您的更新小提琴,我已在script标记中添加了此代码。

参见 UPDATED FIDDLE