gpt4 book ai didi

javascript - 选择单选按钮时显示或隐藏字段 codeigniter

转载 作者:太空宇宙 更新时间:2023-11-04 15:36:01 25 4
gpt4 key购买 nike

我目前正在做一个项目。我有 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>

最佳答案

只需使用.toggle()

.toggle()

Description: Display or hide the matched elements.

With no parameters, the .toggle() method simply toggles the visibility of elements:

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>

关于javascript - 选择单选按钮时显示或隐藏字段 codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com