【问题标题】:How to toggle div visibility using radio buttons?如何使用单选按钮切换 div 可见性?
【发布时间】:2014-12-18 11:57:55
【问题描述】:

我正在做一个项目,我必须在其中切换<div> 的可见性。

我有以下代码:

<input type="radio" name="type" value="1"> Personal
<input type="radio" name="type" value="2"> Business

<div class="business-fields">
    <input type="text" name="company-name">
    <input type="text" name="vat-number">
</div>

我想切换业务领域 div。因此,如果没有选择任何单选按钮或“个人”单选按钮:应该隐藏 div。如果选择了“业务”单选按钮,我希望它显示出来。

目前,我正在使用此代码:

$("input[name='type']").click(function() {
    var status = $(this).val();
    if (status == 2) {
        $(".business-fields").show();
    } else {
        $(".business-fields").hide();
    }
});

但是,我想知道是否可以使用 .toggle() 函数来做到这一点。

【问题讨论】:

  • 为什么不直接使用 .toggle() 而不是 show() 和 hide()?
  • @WeSt 仅当 OP 修改代码以使用 change 事件时才有效。
  • 对我来说.. show() hide() 是最好的方法然后 .toggle() 因为当您重新选择个人时,它会显示文本框..

标签: javascript jquery html


【解决方案1】:

如果可能的话,我通常倾向于不使用 JS,因此这里有一个 HTML+CSS 方式的方法。

.bussines-type .business-fields {
	display: none;
}

.bussines-type input[value="2"]:checked ~ .business-fields {
	display: block;
}
<div class="bussines-type">
	<input id="bt1" type="radio" name="type" value="1">
        <label for="bt1"> Personal</label>
	<input id="bt2" type="radio" name="type" value="2">
        <label for="bt2"> Business</label>

	<div class="business-fields">
		<input type="text" placeholder="Company name" name="company-name">
		<input type="text" placeholder="Vat number" name="vat-number">
	</div>
</div>

~ 代表任何兄弟姐妹,它们位于我们在 ~ 符号之前定义的元素之后。

【讨论】:

    【解决方案2】:

    我建议使用change 事件,并为toggle() 方法提供一个布尔开关,如果开关评估为true,它将显示元素的jQuery 集合,如果评估为则隐藏它们false:

    // select the relevant <input> elements, and using on() to bind a change event-handler:
    $('input[name="type"]').on('change', function() {
      // this, in the anonymous function, refers to the changed-<input>:
      // select the element(s) you want to show/hide:
      $('.business-fields')
          // pass a Boolean to the method, if the numeric-value of the changed-<input>
          // is exactly equal to 2 and that <input> is checked, the .business-fields
          // will be shown:
          .toggle(+this.value === 2 && this.checked);
    // trigger the change event, to show/hide the .business-fields element(s) on
    // page-load:
    }).change();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label>
      <input type="radio" name="type" value="1">Personal</label>
    <label>
      <input type="radio" name="type" value="2">Business</label>
    
    <div class="business-fields">
      <input type="text" name="company-name">
      <input type="text" name="vat-number">
    </div>

    顺便说一句,注意我还包装了相关文本,以指示单选按钮的用途,在 &lt;label&gt; 元素内直接将该文本与 &lt;input&gt; 相关联,因此单击文本会检查 &lt;input&gt;自动。

    参考资料:

    【讨论】:

      【解决方案3】:

      JS Fiddle

      试试这个

      <input type="radio" name="type" value="1" checked ="true"> Personal
      <input type="radio" name="type" value="2"> Business
      
      <div class="business-fields">
          <input type="text" name="company-name">
          <input type="text" name="vat-number">
      </div>
      
      .business-fields{
          display: none;
      }
      
      
      
          $("input[name='type']").change(function() {
              $(".business-fields").toggle();
      });
      

      【讨论】:

      【解决方案4】:

      你可以这样使用:

      $("input[name='type']").change(function() {
          var status = $(this).val();
          if (status != 2) {
              $(".business-fields").hide();
          } else {
              $(".business-fields").show();
          }
      });
      

      【讨论】:

        【解决方案5】:

        .show 和 .hide 非常慢。 https://twitter.com/paul_irish/status/564443848613847040

        最好使用 javascript 打开和关闭 css 类。将类的 css 设置为 {visibility: hidden} 或 {display: none}

        【讨论】:

          【解决方案6】:

          使用下面的代码

          <script>
          $(function(){
          $(":radio[value=1]").click(function(){
              var isVisible = $( ".business-fields" ).is( ":visible" );
              if(isVisible==true)
                  $('.business-fields').toggle();
          });
          
          $(":radio[value=2]").click(function(){
              var isVisible = $( ".business-fields" ).is( ":visible" );
              if(isVisible==false)
                  $('.business-fields').toggle();
          });
          });
          </script>
          

          并且 HTML 是-

          <input name="type" type="radio" value="1" >Personal
          <input type="radio" name="type" value="2" checked="checked"> Business
          
          <div class="business-fields">
              <input type="text" name="company-name">
              <input type="text" name="vat-number">
          </div>
          

          【讨论】:

            【解决方案7】:

            可能是一个更优雅的解决方案,在我看来它更具可读性,并且正如@Ollie_W 指出的那样,切换(显示/隐藏)可能更高效。

            $('input[name="type"]').on('change', function(event) {
              var radioButton = $(event.currentTarget),
                  isBusiness = radioButton.val() === 'business' && radioButton.prop('checked');
              $('.business-fields').toggleClass('hidden', !isBusiness);
            }).change();
            .hidden {
              display: none;
            }
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <label>
              <input type="radio" name="type" value="personal">Personal</label>
            <label>
              <input type="radio" name="type" value="business">Business</label>
            
            <div class="business-fields hidden">
              <input type="text" name="company-name">
              <input type="text" name="vat-number">
            </div>

            【讨论】:

              猜你喜欢
              • 2023-03-24
              • 2020-12-26
              • 2012-12-14
              • 2014-10-05
              • 2013-05-13
              • 2014-07-04
              • 2011-10-28
              • 2014-03-02
              • 1970-01-01
              相关资源
              最近更新 更多