gpt4 book ai didi

显示输入的 jQuery 选项消失

转载 作者:行者123 更新时间:2023-12-01 08:01:43 25 4
gpt4 key购买 nike

我有这个简单的<select>如果选择“其他”选项,则应显示输入。但是,如果我打开/关闭选择,或选择任何其他选项,“其他”就会消失。

这就是我的意思:

<script type="text/javascript">
$(document).ready(function(){
var $milestones = $(".other"),
$selector = $('#source');

$selector.change(function() {
var selected = $selector.find('option:selected').attr('class');

$milestones.hide();
$('#' + selected).show();
});
});
</script>

下面是 HTML,它包含 select 以及隐藏的 div 以显示输入:

<div class="field text">
<label>Where did you hear about us?</label>
<select name="source" id="source">
<option value="Facebook">Facebook</option>
<option value="Twitter">Twitter</option>
<option value="Other" class="other">Other</option>
</select>
</div>
<div class="field text">
<div id="other" class="hideme">
<label>Sources:</label><input type="email">
</div>
</div>

最佳答案

我猜您想在选择 option.other 时显示输入。所以这是代码:

$(document).ready(function(){    
var $selector = $('#source');

$selector.change(function() {
var showOther = $('option.other').is(':selected');
$('#other').toggle(showOther);
});
});

为此,您应该从一开始就隐藏输入,并让 jQuery.toggle()做好它的工作。

.hideme {
display: none;
}

http://jsfiddle.net/kWSrh/

关于显示输入的 jQuery 选项消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329403/

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