views:

12

answers:

1

i would like to disply some fields in case that a radio button was pressed and hide them otherwise. how can i do it?

A: 

Use jQuery to wire up an event handler and implement your desired behavior. MVC doesn't get involved in the browser behavior, but should have logic inside MVC to confirm the HTTP POST from the browser.

Here's a jQuery sample to show/hide optional fields.:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Show/Hide</title>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">

        $(function () {

            $('.option').hide();

            $('input[type=radio]').click(function () {

                $('.option').hide();

                if ($(this).val() == 'M')
                    $('#optionMale').show();
                else
                    $('#optionFemale').show();

            });

        });
    </script>
</head>
<body>
    <div>
        <form method="post" action="foo">

            <div>
                <input type="radio" name="gender" id="genderMale" value="M" />
                <label for="genderMale">Male</label>
            </div>

            <div>
                <input type="radio" name="gender" id="genderFemale" value="F" />
                <label for="genderFemale">Female</label>
            </div>

            <div id="optionMale" class="option">
                <label>Something specific to males</label>
                <input type="text" />
            </div>

            <div id="optionFemale" class="option">
                <label>Something specific to females</label>
                <input type="text" />
            </div>

            <input type="submit" />

        </form>
    </div>
</body>
</html>
AndrewDotHay