admin管理员组文章数量:1352184
I am just wanting to have a div display if a radio box is selected. It's seems simple and most tutes online are for two options, where in my instance I have three.
The logic on this has just been lost on me. Any help would be greatly appreciated.
<!-- // Javascript // -->
<script language="JavaScript" type="text/javascript">
function showhidediv(rad){
var rads=document.getElementsByName(rad.name);
document.getElementById('one').style.display=(rads[1].checked)?'block':'none' ;
document.getElementById('two').style.display=(rads[2].checked)?'block':'none' ;
document.getElementById('three').style.display=(rads[3].checked)?'block':'none' ;
}
</script>
<form name="create" id="create" method="post" enctype="multipart/form-data">
<input name="pagetype" type="radio" value="1" onclick="showhidediv(this);"/>One <br />
<input name="pagetype" type="radio" value="2" onclick="showhidediv(this);"/>two<br>
<input name="pagetype" type="radio" value="3" onclick="showhidediv(this);"/>three<br>
<div id="one" class="CF" style="display:none;" >One </div>
<div id="two" style="display:none;" class="CF" >Two</div>
<div id="three" style="display:none;" class="CF" >Three</div>
</form>
I am just wanting to have a div display if a radio box is selected. It's seems simple and most tutes online are for two options, where in my instance I have three.
The logic on this has just been lost on me. Any help would be greatly appreciated.
<!-- // Javascript // -->
<script language="JavaScript" type="text/javascript">
function showhidediv(rad){
var rads=document.getElementsByName(rad.name);
document.getElementById('one').style.display=(rads[1].checked)?'block':'none' ;
document.getElementById('two').style.display=(rads[2].checked)?'block':'none' ;
document.getElementById('three').style.display=(rads[3].checked)?'block':'none' ;
}
</script>
<form name="create" id="create" method="post" enctype="multipart/form-data">
<input name="pagetype" type="radio" value="1" onclick="showhidediv(this);"/>One <br />
<input name="pagetype" type="radio" value="2" onclick="showhidediv(this);"/>two<br>
<input name="pagetype" type="radio" value="3" onclick="showhidediv(this);"/>three<br>
<div id="one" class="CF" style="display:none;" >One </div>
<div id="two" style="display:none;" class="CF" >Two</div>
<div id="three" style="display:none;" class="CF" >Three</div>
</form>
Share
Improve this question
edited May 15, 2014 at 11:38
Dairo
8181 gold badge9 silver badges24 bronze badges
asked Mar 20, 2013 at 3:27
BrandrallyBrandrally
8493 gold badges14 silver badges24 bronze badges
1
-
3
The variable
rads
is an array, so therefore is zero based, the first entry israds[0]
– kevmc Commented Mar 20, 2013 at 3:33
2 Answers
Reset to default 4<script language="JavaScript" type="text/javascript">
function showhidediv(rad){
var rads=document.getElementsByName(rad.name);
document.getElementById('one').style.display=(rads[0].checked)?'block':'none' ;
document.getElementById('two').style.display=(rads[1].checked)?'block':'none' ;
document.getElementById('three').style.display=(rads[2].checked)?'block':'none' ;
}
</script>
Use this .. The thing i modified is
rads is an array so the first element has an index 0
Radio button index start with 0.
Modify:
function showhidediv( rad )
{
var rads = document.getElementsByName( rad.name );
document.getElementById( 'one' ).style.display = ( rads[0].checked ) ? 'block' : 'none';
document.getElementById( 'two' ).style.display = ( rads[1].checked ) ? 'block' : 'none';
document.getElementById( 'three' ).style.display = ( rads[2].checked ) ? 'block' : 'none';
}
本文标签: formsJavaScriptShow Hide Div gt When Radio Button CheckedStack Overflow
版权声明:本文标题:forms - JavaScriptShow Hide Div > When Radio Button Checked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743908061a2559869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论