【问题标题】:Show/Hide (via Bootstrap's collapse function) divs based on radio button显示/隐藏(在 Bootstrap 折叠功能中)基于单选按钮的 div
【发布时间】:2014-03-14 10:15:28
【问题描述】:

我正在尝试使用 Bootstrap 的折叠功能根据选中的单选按钮显示/隐藏 div。当我不使用 Bootstrap 的折叠功能时,我能够让事情正常工作,但是,为了给人一种更一致的感觉,我想利用这个功能。

这是相关 HTML 的 sn-p:

<div class="col-xs-12 form-group">
  <label class="radio-inline">
    <input type="radio" id="send-now-radio" name="when" value="send-now" checked> <strong>Send Now</strong>
  </label>
  <label class="radio-inline">
    <input type="radio" id="pickup-radio" name="when" value="pickup"> <strong>Hold for pickup</strong>
  </label>
  <label class="radio-inline">
    <input type="radio" id="fax-radio" name="when" value="fax"> <strong>Fax</strong>
  </label>
  <label class="radio-inline">
    <input type="radio" id="email-radio" name="when" value="email"> <strong>Email</strong>
  </label>
</div>
<div class="col-xs-12">
  <div id="send">Send</div>
  <div id="pickup">Pickup</div>
  <div id="fax">Fax</div>
  <div id="email">Email</div>
</div>

这是我的 javascript 代码:

$(document).ready(function()
{
  // Hide all but one method div (since all are shown in case the user has JS disabled)
  $('#send').show();
  $('#pickup').hide();
  $('#fax').hide();
  $('#email').hide();

  // Attach to the radio buttons when they change
  $('#send-now-radio, #pickup-radio, #fax-radio, #email-radio').on('change', function () {
    // Make sure that this change is because a radio button has been checked
    if (!this.checked) return

    // Check which radio button has changed
    if (this.id == 'send-now-radio') {
      $('#send').collapse('show');
      $('#pickup').collapse('hide');
      $('#fax').collapse('hide');
      $('#email').collapse('hide');
    } else if (this.id == 'pickup-radio') {
      $('#send').collapse('hide');
      $('#pickup').collapse('show');
      $('#fax').collapse('hide');
      $('#email').collapse('hide');
    } else if (this.id == 'fax-radio') {
      $('#send').collapse('hide');
      $('#pickup').collapse('hide');
      $('#fax').collapse('show');
      $('#email').collapse('hide');
    } else // if (this.id == 'email-radio') {
      $('#send').collapse('hide');
      $('#pickup').collapse('hide');
      $('#fax').collapse('hide');
      $('#email').collapse('show');
    }
  });
};

这是一个 JS fiddle 的链接:http://jsfiddle.net/DTcHh/156/

不幸的是,我遗漏了一些东西,因为这种行为很奇怪,而不是我所期望的。非常感谢任何帮助。

【问题讨论】:

    标签: javascript html twitter-bootstrap collapse


    【解决方案1】:

    首先,很好的问题。您提供了代码,明确了您的尝试,等等。喜欢它。

    我分叉了你的 JSFiddle,并想出了这个: http://jsfiddle.net/emptywalls/EgVF9/

    这里是 Javascript:

    $('input[type=radio]').on('change', function () {
        if (!this.checked) return
        $('.collapse').not($('div.' + $(this).attr('class'))).slideUp();
        $('.collapse.' + $(this).attr('class')).slideDown();
    });
    

    我不建议使用 Bootstrap 的折叠功能,它依赖于与您需要的完全不同的 DOM 结构。我的小提琴只使用 jQuery 来完成你所需要的。我的方法是将单选按钮和 div 与类配对,这样你就可以干掉你的代码。

    【讨论】:

    • 这不起作用(不再起作用?)。只有第一个会上下滑动
    【解决方案2】:

    正如@emptywalls 提到的,Bootstrap 折叠功能不起作用。我试过了,它几乎可以,除了它是基于 clicking 源元素,而不是它的状态。单选按钮需要注意它的状态。

    但是我想要一些允许我用数据标签标记元素并让它继承功能的东西,就像引导折叠一样。所以我想出了这个:

    <input data-target="#send" data-toggle="radio-collapse" id="send_now_radio" name="when" type="radio" value="send-now">
    <div id="send" class="collapse">Send</div>
    

    然后将其包含在您的网站中一次,它将应用于所有此类按钮。

    $('input[type=radio][data-toggle=radio-collapse]').each(function(index, item) {
      var $item = $(item);
      var $target = $($item.data('target'));
    
      $('input[type=radio][name="' + item.name + '"]').on('change', function() {
        if($item.is(':checked')) {
          $target.collapse('show');
        } else {
          $target.collapse('hide');
        }
      });
    });
    

    这使用Bootstrap的折叠功能进行动画。您可以轻松地使用 jQuery hide() 和 show()。

    【讨论】:

    • 这似乎是一个更清洁的解决方案,可以在 OP 用例之外使用,在我看来做得很好。
    【解决方案3】:

    除非我弄错了,如果您有另一组具有不同名称属性的单选按钮,@jwadsa​​ck 代码将不起作用。

    这是因为$item 和$target 变量是全局声明的。 $item 变量将被最后一组覆盖,只有这个变量会隐藏/显示 collaspe。

    在变量定义之前添加var似乎可以解决问题。

    然后是代码

    $('input[type=radio][data-toggle=radio-collapse]').each(function(index, item) {
      var $item = $(item);
      var $target = $($item.data('target'));
    
     $('input[type=radio][name="' + item.name + '"]').on('change', function() {
       if($item.is(':checked')) {
         $target.collapse('show');
        } else {
         $target.collapse('hide');
       }
     });
    });
    

    【讨论】:

    • 好收获。我会更新我的回复。我手动将咖啡脚本翻译成 javascript 的错误。
    猜你喜欢
    • 2019-02-11
    • 2020-03-03
    • 2018-11-27
    • 2017-04-28
    • 2012-09-18
    • 2019-03-19
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多