jQuery:从选择下拉列表中显示一个元素,在选择其他选项时将其隐藏 [英] jQuery: show an element from select drop down, hide it when other option selected
问题描述
我已经尝试环顾四周,并且存在类似的问题,但是我的方法要简单得多,但是,在这些论坛中我找不到解决方案.
I've tried looking around and there are similar problems, but mine is way more simple, but yet, I can't find a solution within these forums.
在学习jQuery时,我试图在选择选择下拉列表中的项目/选项时显示DIV,并在选择选择下拉列表中的任何其他选项时隐藏相同的DIV.
While learning jQuery, I'm trying to show a DIV when an item/option from a select drop down is selected, and hide that same DIV when any other option in the select drop down is selected.
选择HTML:
<select name="source" id="source">
<option value="null" selected="selected">—Select—</option>
<option value="s1">Source 1</option>
<option value="s2">Source 2</option>
<option value="sother">Other</option>
</select>
DIV我需要时 '其它' 被选择显示:
DIV I need to show when 'Other' is selected:
<div id="specify-source">Other source here...</div>
在选择菜单中选择其他任何选项时,上面的DIV将不可见.
When any other option in the select menu is selected, the above DIV shouldn't be visible.
我已经尝试过这个jQuery,但是当然不能正常工作:
I've tried this jQuery but of course it doesn't work properly:
$(function() {
$.viewMap = {
'sother' : $('#specify-source')
};
$('#source').change(function() {
// hide all
$.each($.viewMap, function() { this.hide(); });
// show current
$.viewMap[$(this).val()].show();
});
});
只要您能给我任何帮助,我将不胜感激.
Any help you can give me, I'd greatly appreciate it.
谢谢
推荐答案
我看到您正在尝试使用视图地图.简化它:
I see what you're trying to do with the view map. Simplify it with:
$('#source').change(function() {
($(this).val() == "sother") ? $('#specify-source').show() : $('#specify-source').hide();
});
这篇关于jQuery:从选择下拉列表中显示一个元素,在选择其他选项时将其隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!