显示/隐藏使用下拉列表 [英] Show/Hide using drop down list
本文介绍了显示/隐藏使用下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试使用以下代码.我喜欢该代码,但我希望默认值为DIV Area1.在下拉菜单中有显示DIV Area 1的HTML代码,但我希望Javascript默认显示DIV Area 1.代码是什么?
Hello I am trying to use this code below. I like the code but I want the default to be DIV Area 1. I have the HTML code showing DIV Area 1 in the drop down menu but I want the Javascript to show DIV AREA 1 by default. What would be the code?
<script type="text/javascript">
$(document).ready(function(){
$('.box').hide();
$('#dropdown').change(function() {
$('.box').hide();
$('#div' + $('#dropdown').val()).show();
});
});
</script>
<form>
<select id="dropdown" name="dropdown">
<option value="0">Choose</option>
<option value="area1" selected="selected">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>
推荐答案
$('.box').hide().first().show();
或者:
$('.box').hide().filter("#divarea1").show();
将以上内容之一放入DOM ready事件:
Put one of the above in the DOM ready event:
$(function(){ /*...*/ });
或
$(document).ready(function(){ /* ... */ });
完整代码:(它应该回答您有关如何显示所选格的下一个问题...)
Full code: (It should answer you next question regarding how to show the selected div...)
$(document).ready(function() {
$('.box').hide().filter("#divarea1").show();
$('#dropdown').change(function() {
var selectedId= $(this).find(':selected').text().replace(/\s/g, "").toLowerCase();
console.log(selectedId);
$('.box').hide().filter('#' + selectedId).show();
});
});
这篇关于显示/隐藏使用下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文