How To Show And Hide Input Fields Based On Radio Button Selection

前端 未结 8 1149

Here is the code to show input fields depending on radio selection like:

SCRIPT



        
相关标签:
8条回答
  • 2020-11-29 01:55

    You'll need to also set the height of the element to 0 when it's hidden. I ran into this problem while using jQuery, my solution was to set the height and opacity to 0 when it's hidden, then change height to auto and opacity to 1 when it's un-hidden.

    I'd recommend looking at jQuery. It's pretty easy to pick up and will allow you to do things like this a lot more easily.

    $('#yesCheck').click(function() {
        $('#ifYes').slideDown();
    });
    $('#noCheck').click(function() {
        $('#ifYes').slideUp();
    });
    

    It's slightly better for performance to change the CSS with jQuery and use CSS3 animations to do the dropdown, but that's also more complex. The example above should work, but I haven't tested it.

    0 讨论(0)
  • 2020-11-29 02:00

    You can do with Very Simple Step

    $(':radio[id=radio1]').change(function() {
       $("#yes").removeClass("none");
       $("#no").addClass("none");
    
    
    });
    $(':radio[id=radio2]').change(function() {
       $("#no").removeClass("none");
       $("#yes").addClass("none");
    
    });
    
    0 讨论(0)
  • 2020-11-29 02:04
    ***This will work.........
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
     window.onload = function() {
        document.getElementById('ifYes').style.display = 'none';
        document.getElementById('ifNo').style.display = 'none';
    }
    function yesnoCheck() {
        if (document.getElementById('yesCheck').checked) {
            document.getElementById('ifYes').style.display = 'block';
            document.getElementById('ifNo').style.display = 'none';
            document.getElementById('redhat1').style.display = 'none';
            document.getElementById('aix1').style.display = 'none';
        } 
        else if(document.getElementById('noCheck').checked) {
            document.getElementById('ifNo').style.display = 'block';
            document.getElementById('ifYes').style.display = 'none';
            document.getElementById('redhat1').style.display = 'none';
            document.getElementById('aix1').style.display = 'none';
       }
    }
    function yesnoCheck1() {
       if(document.getElementById('redhat').checked) {
           document.getElementById('redhat1').style.display = 'block';
           document.getElementById('aix1').style.display = 'none';
        }
       if(document.getElementById('aix').checked) {
           document.getElementById('aix1').style.display = 'block';
           document.getElementById('redhat1').style.display = 'none';
        }
    }
    </script>
    </head>
    <body>
    Select os :<br>
    windows
    <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="yesCheck"/>Unix
    <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="noCheck"/>
    <br>
    <div id="ifYes" style="display:none">
    Windows 2008<input type="radio" name="win" value="2008"/>
    Windows 2012<input type="radio" name="win" value="2012"/>
    </div>
    <div id="ifNo" style="display:none">
    Red Hat<input type="radio" name="unix" onclick="javascript:yesnoCheck1();"value="2008" 
    
    id="redhat"/>
    AIX<input type="radio" name="unix" onclick="javascript:yesnoCheck1();"  
    value="2012" id="aix"/>
    </div>
    <div id="redhat1" style="display:none">
    Red Hat 6.0<input type="radio" name="redhat" value="2008" id="redhat6.0"/>
    Red Hat 6.1<input type="radio" name="redhat" value="2012" id="redhat6.1"/>
    </div>
    <div id="aix1" style="display:none">
    aix 6.0<input type="radio" name="aix" value="2008" id="aix6.0"/>
    aix 6.1<input type="radio" name="aix" value="2012" id="aix6.1"/
    </div>
    </body> 
    </html>***
    
    0 讨论(0)
  • 2020-11-29 02:04

    Using the visibility property only affects the visibility of the elements on the page; they will still be there in the page layout. To completely remove the elements from the page, use the display property.

    display:none // for hiding
    display:block // for showing
    

    Make sure to change your css file to use display instead of visibility too.

    As for the javascript (this is not jQuery), make sure you hide the options by default when the page loads:

    <script type="text/javascript">
        window.onload = function() {
            document.getElementById('ifYes').style.display = 'none';
        }
    
        function yesnoCheck() {
            if (document.getElementById('yesCheck').checked) {
                document.getElementById('ifYes').style.display = 'block';
            } 
            else {
                document.getElementById('ifYes').style.display = 'none';
            }
        }
    
    </script>
    

    If you haven't done so already, I would recommend taking a look at jQuery. jQuery code is much clearer and easier to write and understand.

    http://www.w3schools.com/jquery/default.asp

    0 讨论(0)
  • 2020-11-29 02:08

    Replace all instances of visibility style to display

    display:none //to hide
    display:block //to show
    

    Here's updated jsfiddle: http://jsfiddle.net/QAaHP/16/

    You can do it using Mootools or jQuery functions to slide up/down but if you don't need animation effect it's probably too much for what you need.

    CSS display is a faster and simpler approach.

    0 讨论(0)
  • 2020-11-29 02:12
    <script type="text/javascript">
    function Check() {
        if (document.getElementById('yesCheck').checked) {
            document.getElementById('ifYes').style.display = 'block';
        } 
        else {
            document.getElementById('ifYes').style.display = 'none';
    
       }
    }
    
    </script>
    </head>
    <body>
    Select os :
    <br>
    Two
    <input type="radio" onclick="Check();" value="Two" name="categor`enter code here`y" id="yesCheck"/>One
    <input type="radio" onclick="Check();" value="One"name="category"/>
    <br>
    <div id="ifYes" style="display:none" >
    Three<input type="radio" name="win" value="Three"/>
    Four<input type="radio" name="win" value="Four"/>
    
    0 讨论(0)
提交回复
热议问题