I have a dropdown box to list 2015/2016 years. Each is provided as a div, under which it has radio buttons for (Monthly & Weekly).
If i select 2015 in dropdown - 2015 Monthly & Weekly should be view. Same for 2016.
My issue is, both the year's div are viewed together in same page. Could someone give a suggestion please. Thanks for your time and help.
My JS Code:
<!DOCTYPE html> <html> <body> <select class="dropdown" id="year" name="year" size="1" onchange="changeDiv('year');"> <option value="2015" selected="selected">2015</option> <option value="2016">2016</option> </select> <div id="2015"> <B>AAAAAAA</B> <input type="radio" checked="checked" onclick="javascript:changeDiv('2015Monthly');" />Monthly <input type="radio" onclick="javascript:changeDiv('2015Weekly');" align="left" />Weekly <div id="2015Monthly" ><B>2015 - Monthly</B></div> <div id="2015Weekly" ><B>2015 - Weekly</B></div> <div id="2016"> <B>HHHHHHH</B> <input type="radio" checked="checked" onclick="javascript:changeDiv('2016Monthly');" />Monthly <input type="radio" onclick="javascript:changeDiv('2016Weekly');" />Weekly <div id="2016Monthly" ><B>2016 - Monthly</B></div> <div id="2016Weekly" ><B>2016 - Weekly</B></div> <script> function changeDiv(divId) { if(divId=='2015Monthly') { document.getElementById(divId).style.display="block"; document.getElementById('2015Weekly').style.display="none"; }else if(divId=='2015Weekly') { document.getElementById(divId).style.display="block"; document.getElementById('2015Monthly').style.display="none"; }else if(divId=='2016Monthly') { document.getElementById(divId).style.display="block"; document.getElementById('2016Weekly').style.display="none"; }else if(divId=='2016Weekly') { document.getElementById(divId).style.display="block"; document.getElementById('2016Monthly').style.display="none"; }else if(divId=='2016') { document.getElementById(divId).style.visibility="visible"; document.getElementById('2015').style.visibility="hidden"; }else if(divId=='2015') { document.getElementById(divId).style.visibility="visible"; document.getElementById('2016').style.visibility="hidden"; } } </script> </body> </html>
Output:
The (main) problem is that instead of passing the id
your pass the string year
.
So you need to change the onchange
value to this.value
so it will take the value of the selection (2015/2015).
<!DOCTYPE html> <html> <body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="dropdown" id="year" name="year" size="1" onchange="changeDiv(this.value);"> <option value="2015" selected="selected">2015</option> <option value="2016">2016</option> </select> <div id="2015"> <B>AAAAAAA</B> <input type="radio" checked="checked" onclick="javascript:changeDiv('2015Monthly');" name="2015_type" />Monthly <input type="radio" onclick="javascript:changeDiv('2015Weekly');" align="left" name="2015_type" />Weekly <div id="2015Monthly" ><B>2015 - Monthly</B></div> <div id="2015Weekly" ><B>2015 - Weekly</B></div> </div> <div id="2016"> <B>HHHHHHH</B> <input type="radio" checked="checked" onclick="javascript:changeDiv('2016Monthly');" name="2016_type" />Monthly <input type="radio" onclick="javascript:changeDiv('2016Weekly');" name="2016_type" />Weekly <div id="2016Monthly" ><B>2016 - Monthly</B></div> <div id="2016Weekly" ><B>2016 - Weekly</B></div> </div> <script> function changeDiv(divId) { if(divId=='2015Monthly') { document.getElementById(divId).style.display="block"; document.getElementById('2015Weekly').style.display="none"; }else if(divId=='2015Weekly') { document.getElementById(divId).style.display="block"; document.getElementById('2015Monthly').style.display="none"; }else if(divId=='2016Monthly') { document.getElementById(divId).style.display="block"; document.getElementById('2016Weekly').style.display="none"; }else if(divId=='2016Weekly') { document.getElementById(divId).style.display="block"; document.getElementById('2016Monthly').style.display="none"; }else if(divId=='2016') { document.getElementById(divId).style.display="block"; document.getElementById('2015').style.display="none"; }else if(divId=='2015') { document.getElementById(divId).style.display="block"; document.getElementById('2016').style.display="none"; } } $('#year').change(); $(':checked').trigger('click'); </script> </body> </html>
There are some thimgs that you are missing. For example how call the function on change. The parameter of function is not the correct one. This solution should work:
<!DOCTYPE html>
<html>
<body>
<select id="mySelect" name="year" size="1" onchange="myFunction()">
<option value="2015" selected="selected">2015</option>
<option value="2016">2016</option>
</select>
<div id="2015" style="display:block">
<B>AAAAAAA</B>
<input type="radio" checked="checked" onclick="javascript:changeDiv('2015Monthly');" />Monthly
<input type="radio" onclick="javascript:changeDiv('2015Weekly');" align="left" />Weekly
<div id="2015Monthly" ><B>2015 - Monthly</B></div>
<div id="2015Weekly" ><B>2015 - Weekly</B></div>
</div>
<div id="2016" style="display:none">
<B>HHHHHHH</B>
<input type="radio" checked="checked" onclick="javascript:changeDiv('2016Monthly');" />Monthly
<input type="radio" onclick="javascript:changeDiv('2016Weekly');" />Weekly
<div id="2016Monthly" ><B>2016 - Monthly</B></div>
<div id="2016Weekly" ><B>2016 - Weekly</B></div>
</div>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
if(x=='2016')
{
document.getElementById(x).style.display="block";
document.getElementById('2015').style.display="none";
}else if(x=='2015')
{
document.getElementById(x).style.display="block";
document.getElementById('2016').style.display="none";
}
}
</script>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.