【问题标题】:Show or Hide field when selected radio button codeigniter选择单选按钮代码点火器时显示或隐藏字段
【发布时间】:2017-11-04 21:12:06
【问题描述】:

我目前正在做一个项目。我有 2 个单选按钮,1)一种方式 2)往返。当用户尝试选择单向单选按钮时,返回文本字段将隐藏。

我看到了一个线程和有人评论这个问题。场景:我选择了单向单选按钮,返回字段将消失,是的,它正在工作,但有一些问题。如果我改变主意,从单向单选按钮更改为往返行程怎么办?问题是返回字段没有回来

**查看**

//我的单选按钮

<div class="pure-u-1-1 radiobtn">
     <form action="">
           <input type="radio" name="flight_type"  value="one_way" class="onew" style=""  >One Way
           <input type="radio" name="flight_type" class="roundw" style="" checked>Round Trip
     </form>
</div>

// 将隐藏/显示的返回字段

<div class="pure-u-1-1 dr" id="try">
      <label for="return" class="drr">Return</label>
      <input type="text" id="return" name="return" class="departreturn"><br>

</div>

JS

<script type="text/javascript">
$(document).on('change', 'input:radio[name=flight_type]', function(){
    $('div[id^="try"]').hide(); // hide all DIVs begining with "my_radio_"
    $('#' + $(this).attr('id') + '_text').show(); // show the current one
});

</script>

【问题讨论】:

    标签: javascript php codeigniter


    【解决方案1】:

    只需使用.toggle()

    .toggle()

    说明:显示或隐藏匹配的元素。

    没有参数,.toggle() 方法只是切换元素的可见性:

    参考号:http://api.jquery.com/toggle/

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="pure-u-1-1 radiobtn">
      <form action="">
        <input type="radio" name="flight_type" value="one_way" class="onew" style="">One Way
        <input type="radio" name="flight_type" class="roundw" style="" checked>Round Trip
      </form>
    </div>
    
    <div class="pure-u-1-1 dr" id="try">
      <label for="return" class="drr">Return</label>
      <input type="text" id="return" name="return" class="departreturn"><br>
    
    </div>
    
    
    <script type="text/javascript">
      $(document).on('change', 'input:radio[name=flight_type]', function() {
        $('div[id^="try"]').toggle(); // toggle all DIVs begining with "my_radio_"
        $('#' + $(this).attr('id') + '_text').show(); // show the current one
      });
    </script>

    【讨论】:

    • 您好,先生,我有一个问题,当我选择 One trip 时,我该怎么做才能阻止验证显示“需要返回字段”?
    猜你喜欢
    • 2021-10-01
    • 2016-03-13
    • 2021-02-16
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多