2017-06-03 74 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

相關問題