我想显示/隐藏元素取决于下拉菜单中选择的内容,我得到了它,但我想要在选择DIVAREA2或DIVAREA3时显示div。说,我希望DIV区域4显示在DiV区域2& DIV区3
从这里提供了一些代码,我发现它接近我想做的事情。这是jsfiddle:
http://jsfiddle.net/Ayeblinken/guDsm/1/
HTML
<form>
<select id="dropdown" name="dropdown">
<option value="0">Choose</option>
<option value="area1">DIV Area 1</option>
<option value="area2">DIV Area 2</option>
<option value="area3">DIV Area 3</option>
</select>
</form>
<div id="divarea1" class="box">DIV Area 1</div>
<div id="divarea2" class="box">DIV Area 2</div>
<div id="divarea3" class="box">DIV Area 3</div>
<div id="divarea4" class="box">DIV Area 4</div>
的JavaScript
$(document).ready(function() {
$('.box').hide();
$('#dropdown').change(function() {
$('.box').hide();
$('#div' + $(this).val().show();
});
});
答案 0 :(得分:3)
如果你想要多个节目,请使用如下类:
<form>
<select id="dropdown" name="dropdown">
<option value="0">Choose</option>
<option value="area1">DIV Area 1</option>
<option value="area2">DIV Area 2</option>
<option value="area3">DIV Area 3</option>
</select>
</form>
<div class="box area1">DIV Area 1</div>
<div class="box area1">DIV Area 2</div>
<div class="box area2">DIV Area 3</div>
<div class="box area2">DIV Area 4</div>
$(document).ready(function(){
$('.box').hide();
$('#dropdown').change(function() {
$('.box').hide();
$('.' + $(this).val()).show();
});
});
这是一个JSFIDDLE:http://jsfiddle.net/mac1175/yEJL2/
要默认选择area1,请将selected
属性添加到区域1的选项中。将链接触发器调用添加到更改事件处理程序,如...
$(document).ready(function(){
$('.box').hide();
$('#dropdown').change(function() {
$('.box').hide();
$('.' + $(this).val()).show();
}).trigger('change');
});
JSFIDDLE:http://jsfiddle.net/mac1175/FKf9p/
答案 1 :(得分:0)
if语句怎么样?
$(document).ready(function(){
$('.box').hide();
$('#dropdown').change(function() {
var area = $(this).val();
$('.box').hide();
$('#div' + area).show();
if(area == 'area2' || area== 'area3') {
$('#divarea4').show();
}
});
});