[英]Toggle Show/Hide DIV by ID selector using JS and Dropwdown
I have a dropdown menu, when a selection is made, it will show a div by ID selector.我有一个下拉菜单,当进行选择时,它会按 ID 选择器显示一个 div。
When "1 Subscriber" is selected, it will display the div with the ID "sub1".选择“1订阅者”时,它将显示ID“SUB1”的DIV。 When "2 Subscriber" is selected, it will display the div with the ID "sub1" & "sub2".
选择“2用户”时,它将显示ID“Sub1”和“Sub2”的DIV。
The issue is that when "2 Subscriber" is selected, it will show the divs with ID's "sub1", "sub2", "sub3", "sub4", "sub5". The issue is that when "2 Subscriber" is selected, it will show the divs with ID's "sub1", "sub2", "sub3", "sub4", "sub5".
What am I doing wrong, please help?我做错了什么,请帮忙?
Thanks!谢谢! :)
:)
function showHideSubscribers(sel) { var value = sel.value; if(value==0) document.getElementById('sub1').style.display = 'block'; if(value==1) document.getElementById('sub1').style.display = 'block'; document.getElementById('sub2').style.display = 'block'; if(value==2) document.getElementById('sub1').style.display = 'block'; document.getElementById('sub2').style.display = 'block'; document.getElementById('sub3').style.display = 'block'; if(value==3) document.getElementById('sub1').style.display = 'block'; document.getElementById('sub2').style.display = 'block'; document.getElementById('sub3').style.display = 'block'; document.getElementById('sub4').style.display = 'block'; if(value==4) document.getElementById('sub1').style.display = 'block'; document.getElementById('sub2').style.display = 'block'; document.getElementById('sub3').style.display = 'block'; document.getElementById('sub4').style.display = 'block'; document.getElementById('sub5').style.display = 'block'; };
<select name="showHideSubscribers" id="showHideSubscribers" onchange="showHideSubscribers(this)"> <option value="0" selected="selected">1 Subscriber</option> <option value="1">2 Subscribers</option> <option value="2">3 Subscribers</option> <option value="3">4 Subscribers</option> <option value="4">5 Subscribers</option> </select> <div id="sub1" style="display:none">1 Subscriber</div> <div id="sub2" style="display:none">2 Subscriber</div> <div id="sub3" style="display:none">3 Subscriber</div> <div id="sub4" style="display:none">4 Subscriber</div> <div id="sub5" style="display:none">5 Subscriber</div> </div>
You missed {}
on the if blocks you may not use them only when there is one statement in the if block:您错过了 if 块上的
{}
,只有当 if 块中有一个语句时才可能不使用它们:
function showHideSubscribers(sel) {
var value = sel.value;
console.log(value)
if(value==0)
document.getElementById('sub1').style.display = 'block';
if(value==1){
document.getElementById('sub1').style.display = 'block';
document.getElementById('sub2').style.display = 'block';
}
if(value==2){
document.getElementById('sub1').style.display = 'block';
document.getElementById('sub2').style.display = 'block';
document.getElementById('sub3').style.display = 'block';
}
if(value==3){
document.getElementById('sub1').style.display = 'block';
document.getElementById('sub2').style.display = 'block';
document.getElementById('sub3').style.display = 'block';
document.getElementById('sub4').style.display = 'block';
}
if(value==4){
document.getElementById('sub1').style.display = 'block';
document.getElementById('sub2').style.display = 'block';
document.getElementById('sub3').style.display = 'block';
document.getElementById('sub4').style.display = 'block';
document.getElementById('sub5').style.display = 'block';
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.