我對使用Razor進行編程非常非常新。我有一些使用HTML的經驗,但我在某個領域遇到了麻煩。我只是試圖建立一個類似調查的頁面,現在我有一個要求用戶姓名的表單,並且有三個不同選項的單選按鈕選擇。我想要一個基於用戶選擇的單選按鈕顯示的複選框。然後在信息完成時,底部會有一個提交按鈕。我一直在閱讀關於使用「隱藏」屬性,但不知道如何使用它。以下是我迄今爲止:使用Razor顯示取決於單選按鈕選擇的複選框形式
@{
//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>
我完全失去了,我正在嘗試使用JavaScript,但我無法找到一個對我有意義的示例。對JavaScript不熟悉,使得這更加困難。我已決定開始顯示所有複選框,但禁用它們,並試圖根據收音機選擇啓用,但沒有運氣!任何幫助,將不勝感激。 – user3042572