admin管理员组文章数量:1026925
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>
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>
Share
Improve this question
edited Jun 5, 2012 at 18:20
gdoron
150k59 gold badges302 silver badges371 bronze badges
asked Jun 5, 2012 at 18:01
G-YoG-Yo
981 silver badge9 bronze badges
3 Answers
Reset to default 5$('.box').hide().first().show();
Or:
$('.box').hide().filter("#divarea1").show();
Live DEMO
Put one of the above in the DOM ready event:
$(function(){ /*...*/ });
Or
$(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();
});
});
could do this...
$('.box').hide().filter(':first').show();
A lot of plicated answers for a simple problem:
$('.box:gt(0)').hide();
I'd code it up like this:
$(document).ready(function(){
$('.box:gt(0)').hide();
$('#dropdown').change(function() {
$('.box:visible').hide();
if ($(this).prop('selectedIndex')>0)
$('.box').eq($(this).prop('selectedIndex')-1).show();
});
});
http://jsfiddle/lucuma/xNZWY/
If you remove the 1st option from your dropdown (since you are preselecting is it necessary?) it bees a little simpler since we can remove the if
$(document).ready(function(){
$('.box:gt(0)').hide();
$('#dropdown').change(function() {
$('.box:visible').hide();
$('.box').eq($(this).prop('selectedIndex')-1).show();
});
});
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>
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>
Share
Improve this question
edited Jun 5, 2012 at 18:20
gdoron
150k59 gold badges302 silver badges371 bronze badges
asked Jun 5, 2012 at 18:01
G-YoG-Yo
981 silver badge9 bronze badges
3 Answers
Reset to default 5$('.box').hide().first().show();
Or:
$('.box').hide().filter("#divarea1").show();
Live DEMO
Put one of the above in the DOM ready event:
$(function(){ /*...*/ });
Or
$(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();
});
});
could do this...
$('.box').hide().filter(':first').show();
A lot of plicated answers for a simple problem:
$('.box:gt(0)').hide();
I'd code it up like this:
$(document).ready(function(){
$('.box:gt(0)').hide();
$('#dropdown').change(function() {
$('.box:visible').hide();
if ($(this).prop('selectedIndex')>0)
$('.box').eq($(this).prop('selectedIndex')-1).show();
});
});
http://jsfiddle/lucuma/xNZWY/
If you remove the 1st option from your dropdown (since you are preselecting is it necessary?) it bees a little simpler since we can remove the if
$(document).ready(function(){
$('.box:gt(0)').hide();
$('#dropdown').change(function() {
$('.box:visible').hide();
$('.box').eq($(this).prop('selectedIndex')-1).show();
});
});
本文标签: javascriptShowHide using drop down listStack Overflow
版权声明:本文标题:javascript - ShowHide using drop down list - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745652537a2161404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论