JavaScript / Show Hide Div > When Radio Button Che

2019-07-10 19:04发布

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>

2条回答
SAY GOODBYE
2楼-- · 2019-07-10 19:19

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';
    }
查看更多
你好瞎i
3楼-- · 2019-07-10 19:28
<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

查看更多
登录 后发表回答