【问题标题】:How to make a show hide of two textboxes using two radio buttons如何使用两个单选按钮隐藏两个文本框
【发布时间】:2016-01-29 04:45:36
【问题描述】:

通过单击单选按钮,我需要显示隐藏两个文本框。当我点击radio1文本框1显示.. 我需要在单击单选按钮2时显示文本框2并隐藏文本框1

请帮我用下面的代码解决

HTML

   <p>
      KYC<input type="radio" name="radio1" value="Show">
      Bank OTP<input type="radio" name="radio2" value="hide">
  </p>
  <div class="textd1">
     <p>Textbox #1 <input type="text" name="text1" id="text1" maxlength="30"></p>
 </div>
 <div class="textd2">
 <p>Textbox #2 <input type="text" name="text2" id="text2" maxlength="30">/p>
 </div>

js

   $(document).ready(function() {
        $(".textd1").hide()
        $(".textd2").hide()
        $('[name=radio1]').on('change', function(){
            $('.textd1').toggle(this.value === 'Show');
        }) 
        $('[name=radio2]').on('change', function(){
            $('.textd2').toggle(this.value === 'Show');
        })
    });

【问题讨论】:

    标签: javascript html textbox radio-button show-hide


    【解决方案1】:

    保持单选按钮的name 相同,以便它形成一个组,并根据click 的单个event 并根据radio 的值显示或隐藏所需的文本框,如下所示:

    $(document).ready(function() {
      $(".textd1").hide()
      $(".textd2").hide()
      $('[name=radio]').on('click', function() {
        if ($(this).val() == "Show") {
          $('.textd1').show();
          $('.textd2').hide();
        } else {
          $('.textd1').hide();
          $('.textd2').show();
        }
      })
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>
      KYC
      <input type="radio" name="radio" value="Show">Bank OTP
      <input type="radio" name="radio" value="hide">
    </p>
    
    <div class="textd1">
      <p>Textbox #1
        <input type="text" name="text1" id="text1" maxlength="30">
      </p>
    </div>
    <div class="textd2">
      <p>Textbox #2
        <input type="text" name="text2" id="text2" maxlength="30">
      </p>
    </div>

    【讨论】:

    • 感谢它的工作.. 我如何设置两个单选按钮在加载时未选中?
    • 我怎样才能给文本框一个小动画
    • @APJ 而不是 hideshow 使用 fadeInfadeOut 就像 $('.textd1').fadeIn();$('.textd2').fadeOut(); 一样,反之亦然。您还可以为 fadeInfadeOut$('.textd1').fadeIn('200');$('.textd2').hide('fast');
    【解决方案2】:

    试试这个方法

    $(document).ready(function() {
        $(".textd1").hide()
        $(".textd2").hide()
        $('#a').on('change', function(){
            $(".textd2").hide()
          $('.textd1').show();
        }) 
        $('#b').on('change', function(){
            $(".textd1").hide()
            $('.textd2').show()
        })
    });
    

    https://jsfiddle.net/d1n4rg24/1/

    【讨论】:

      猜你喜欢
      • 2022-07-01
      • 2022-07-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 2020-03-03
      • 2015-07-04
      相关资源
      最近更新 更多