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 is rads[0] – kevmc Commented Mar 20, 2013 at 3:33
Add a ment  | 

2 Answers 2

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