【发布时间】:2014-07-28 21:02:31
【问题描述】:
我使用 jquery 选择框显示/隐藏 div。这可行,但是当返回/单击以清空选项值(Choose Color)时,jquery 不会隐藏最后一个 div。
JS:
$(document).ready(function(){
$("select").change(function(){
$( "select option:selected").each(function(){
if($(this).attr("value")=="red"){
$(".box").hide();
$(".red").show();
}
if($(this).attr("value")=="green"){
$(".box").hide();
$(".green").show();
}
if($(this).attr("value")=="blue"){
$(".box").hide();
$(".blue").show();
}
});
}).change();
});
HTML:
<div>
<select>
<option>Choose Color</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
<div class="red box" >You have selected <strong>red option</strong> so i am here</div>
<div class="green box">You have selected <strong>green option</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue option</strong> so i am here</div>
CSS:
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
如何解决这个问题?
【问题讨论】:
-
那是因为你没有隐藏它的代码...
-
@Shahar:在 Q 中添加 css 更新。
标签: javascript jquery html css