<div class="DDLabel">
Month:@Html.DropDownList("Month"new SelectList(new List<object>
{
new { value = 1 , text = "Jan"},
new { value = 2 , text = "Feb"},
new { value = 3 , text = "Mar"},
new { value = 4 , text = "Apr"},
new { value = 5 , text = "May"},
new { value = 6 , text = "Jun"},
new { value = 7 , text = "Jul"},
new { value = 8 , text = "Aug"},
new { value = 9 , text = "Sep"},
new { value = 10 , text = "Oct"},
new { value = 11, text = "Nov"},
new { value = 12 , text = "Dec"},
}, "value", "text", 1), new { @onchange = "e_date_changed();", @style = "width:66px" })
</div>
<div class="DDLabel">
Day:@Html.DropDownList("Day", new SelectList(new List<object>
{
new { value =1, text = "1"},
new { value =2, text = "2"},
new { value =3 ,text ="3"},
new { value =4, text = "4"},
new { value =5, text = "5"},
new { value =6, text = "6"},
new { value =7, text = "7"},
new { value =8, text = "8"},
new { value =9, text = "9"},
new { value =10, text = "10"},
new { value =11, text = "11"},
new { value =12, text = "12"},
new { value =13, text = "13"},
new { value =14, text = "14"},
new { value =15, text = "15"},
new { value =16, text = "16"},
new { value =17, text = "17"},
new { value =18, text = "18"},
new { value =19, text = "19"},
new { value =20, text = "20"},
new { value =21, text = "21"},
new { value =22, text = "22"},
new { value =23, text = "23"},
new { value =24, text = "24"},
new { value =25, text = "25"},
new { value =26, text = "26"},
new { value =27, text = "27"},
new { value =28, text = "28"},
new { value =29, text = "29"},
new { value =30, text = "30"},
new { value =31, text = "31"},
}, "value", "text", 1), new { @onchange = "e_date_changed();", @style = "width:66px" })
</div>
我想要if条件。如果用户选择4月份用户,那么下一个DropDownList中的用户必须仅显示30天,如果是10月那么它应该是31天。我该怎么做?这个代码放在哪里?
答案 0 :(得分:0)
这将在客户端代码中完成,而不是服务器端代码。既然你在评论中提到jQuery,我会假设使用它。您没有显示客户端标记,因此为简单起见,我还将假设id
元素上的select
属性。
首先,您需要一个“月”select
的更改事件处理程序,如下所示:
$('#month').change(function () {
var selectedMonth = $(this).val();
// respond to the new value here
});
只要select
元素的选定值发生变化,就会调用此方法。然后在此功能中,您将响应新值并相应地调整“日期”select
。由于月份长度没有太多的模式,我想它会归结为switch
语句。类似的东西:
switch (selectedMonth) {
case 4:
case 6:
case 9:
case 11:
// 30 days
break;
case 2:
// 28 or 29 days, good luck with that
break;
default:
// 31 days
}
您可以根据年份查找确定2月份天数的逻辑。通常每4年一次,但有一些例外。
至于修改“日”select
,有一些不同的方法。例如,您可能有一些明显不同的select
元素用于显示/隐藏的“日期”,以便在任何给定时间仅显示“正确”的元素。这可能是这样的:
$('#day28').hide();
$('#day29').hide();
$('#day30').hide();
$('#day31').show();
或者,您可能在代码中包含一些包含option
元素的预定义HTML字符串,并且每次只需remove和append整个元素集。这可能看起来像这样:
$('#day option').remove();
$('#day').append(options31string);
或者也许是构建select
元素的其他方法。无论您如何操作,重点是您要修改它以响应第一个change
元素的select
事件。这通常被称为“级联下拉”(您可以在Google上搜索许多其他示例和插件)。
但老实说,只是使用类似a date picker plugin之类的东西听起来更容易。