2017-06-03 79 views
1

我目前正在做一个项目。我有2个单选按钮,1)单向2)往返。当用户试图选择单向单选按钮时,返回文本字段将被隐藏。选中单选按钮时显示或隐藏字段codeigniter

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

** **查看

//我的单选按钮

<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> 

回答

1

只需使用.toggle()

.toggle()

描述:显示或隐藏匹配元素。

不带参数的.toggle()方法简单地切换元素的可见性:

REF: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>

+0

喜先生,我有一个问题,是什么我可以做什么来防止每当我选择一次旅行时显示“需要返回字段”的验证? – Angel

相关问题