显示复选框表单取决于使用Razor选择单选按钮

时间:2014-01-21 02:28:09

标签: .net razor

我对使用Razor进行编程非常非常新。我有一些HTML的经验,但我特别在一个领域遇到麻烦。我只是想设置一个类似调查的页面,现在我有一个表格要求用户的名字,并有一个单选按钮选择有三个不同的选项。我希望根据用户选择的单选按钮显示一个复选框。然后在底部,信息完成后会有一个提交按钮。我一直在阅读使用“隐藏”属性,但不知道如何利用它。以下是我到目前为止的情况:

@{
//capture the selected value
var userName = Request["yourname"];
var meal = Request["favoritemeal"];
var mealChoices = Request["favoritebreakfast"];
}
<!DOCTYPE html>
<html>  
<head>
    <title>Meals On Us Food Survey</title>
</head>
<body>
    <h1>Meals On Us Food Survey</h1>
    <!-- code dynamically generated HTML in the appropriate place -->
    @if (IsPost)
    { 
        //check to see if you have a value to use
        if (userName.IsEmpty())
        { 
            <p>You haven't entered a name.</p>
        }
        else if (meal.IsEmpty())
        {
            <p>You haven't entered a favorite meal.</p>
        }
        else
        {
            <p>Greetings <strong>@userName</strong>.</p>
            <p>Your favorite meal is <strong>@meal</strong>.</p>
        }

    }

    <!-- For radio buttons, ensure that the "name=" value is the same for your group -->
    <form method="post" action="">
        <label for="yourname">Your Name:</label>
        <input type="text" id="yourname" name="yourname" /> <br />
        What is your favorite meal?<br />
        <input type="radio" name="favoritemeal" value="Breakfast" />Breakfast<br />
        <input type="radio" name="favoritemeal" value="Lunch" />Lunch<br />
        <input type="radio" name="favoritemeal" value="Supper" />Supper<br />

编辑:我正在尝试更多地了解代码,我有这个,但不确定我哪里出错......

@{
//capture the selected value
var userName = Request["yourname"];
var meal = Request["favoritemeal"];
var mealChoices = Request["favoritebreakfast"];
}
<!DOCTYPE html>
<html>  
<head>
    <title>Meals On Us Food Survey</title>
</head>
<body>
    <h1>Meals On Us Food Survey</h1>
    <!-- code dynamically generated HTML in the appropriate place -->
    @if (IsPost)
    { 
        //check to see if you have a value to use
        if (userName.IsEmpty())
        { 
            <p>You haven't entered a name.</p>
        }
        else if (meal.IsEmpty())
        {
            <p>You haven't entered a favorite meal.</p>
        }
        else
        {
            <p>Greetings <strong>@userName</strong>.</p>
            <p>Your favorite meal is <strong>@meal</strong>.</p>
        }

    }
    <script type ="text/javascript">
        function setMealSelection() {
            var mealSelection = document.getElementById("breakfastChoice");
            if (mealSelection.checked) {
                document.getElementById("breakfastSelection").disabled = false;
            }
        }

        </script>
    <!-- For radio buttons, ensure that the "name=" value is the same for your group -->
    <form method="post" action="">
        <label for="yourname">Your Name:</label>
        <input type="text" id="yourname" name="yourname" /> <br />
        What is your favorite meal?<br />
        <input type="radio" name="favoritemeal" value="Breakfast" id="breakfastChoice"/>Breakfast<br />
        <input type="radio" name="favoritemeal" value="Lunch" />Lunch<br />
        <input type="radio" name="favoritemeal" value="Supper" />Supper<br />



        Please choose your favorite breakfast items:  <br />
        <input type="checkbox" id="breakfastSelection" name="breakfastitems" value="Eggs" disabled/>Eggs<br />
        <input type="checkbox" name="breakfastitems" value="Bacon" disabled/>Bacon<br />
        <input type="checkbox" name="breakfastitems" value="Waffles" disabled/>Waffles<br />
        <input type="checkbox" name="breakfastitems" value="Ham" disabled/>Ham<br />
        <input type="checkbox" name="breakfastitems" value="Sausage" disabled/>Sausage<br />
        <input type="checkbox" name="breakfastitems" value="Toast" disabled/>Toast<br />
        <input type="checkbox" name="breakfastitems" value="Fruitcup" disabled/>Fruitcup<br />

        Please choose your favorite lunch items:  <br />
        <input type="checkbox" name="lunchitems" value="Grilled Cheese" disabled/>Grilled Cheese<br />
        <input type="checkbox" name="lunchitems" value="French Fries" disabled/>French Fries<br />
        <input type="checkbox" name="lunchitems" value="Soup" disabled/>Soup<br />
        <input type="checkbox" name="lunchitems" value="Salad" disabled/>Salad<br />
        <input type="checkbox" name="lunchitems" value="Zoodles" disabled/>Zoodles<br />
        <input type="checkbox" name="lunchitems" value="Sandwich" disabled/>Sandwich<br />
        <input type="checkbox" name="lunchitems" value="Potato Chips" disabled/>Potato Chips<br />
        <input type="checkbox" name="breakfastitems" value="Fruitcup" disabled/>Fruitcup<br />

        Please choose your favorite supper items:  <br />
        <input type="checkbox" name="supperitems" value="Steak" disabled/>Steak<br />
        <input type="checkbox" name="supperitems" value="Lobster" disabled/>Lobster<br />
        <input type="checkbox" name="supperitems" value="Pork Chops" disabled/>Pork Chops<br />
        <input type="checkbox" name="supperitems" value="Chicken" disabled/>Chicken<br />
        <input type="checkbox" name="supperitems" value="Ribs" disabled/>Ribs<br />
        <input type="checkbox" name="supperitems" value="Baked Potato" disabled/>Baked Potato<br />
        <input type="checkbox" name="supperitems" value="Wheel of Cheese" disabled/>Wheel of Cheese<br />
            <input type="submit" value="submit" />
        </form>

1 个答案:

答案 0 :(得分:0)

您可以根据某些条件轻松使用jquery / javascript和css来显示/隐藏元素。 Here是使用jquery的示例。