我希望一旦用户为DropDownList选择一个值,只要它不是'Custom'就强制提交。如果是“自定义”,我不希望提交表单。相反,我想让startingDate和endingDate显示。但是,除非选择了Custom,否则我不希望显示startingDate或endingDate。我想这必须用jQuery或JavaScript来完成。有谁能告诉我如何实现这个目标?
以下是控制器中的代码,用于将下拉菜单的开始日期,结束日期和列表传递给视图:
List<SelectListItem> rangeList = new List<SelectListItem>();
rangeList.Add(new SelectListItem { Text = "Today", Value = "Today" });
rangeList.Add(new SelectListItem { Text = "Yesterday", Value = "Yesterday" });
rangeList.Add(new SelectListItem { Text = "Past 7 Days", Value = "Past 7 Days" });
rangeList.Add(new SelectListItem { Text = "Past 30 Days", Value = "Past 30 Days" });
rangeList.Add(new SelectListItem { Text = "Last Month", Value = "Last Month" });
rangeList.Add(new SelectListItem { Text = "Custom", Value = "Custom" });
ViewBag.rangeList = rangeList;
ViewBag.startingDate = startingDate;
ViewBag.endingDate = endingDate;
ViewBag.specifiedRange = specifiedRange;
修改
我修改了我的代码以显示添加脚本的尝试。下面是我现在在视图中的代码。它没有做任何事情。
<script type="text/javascript">
$("#range").change(function ()
{
if ($(this).val() == "Custom")
{
$("p.down").toggle();
}
else
{
$("form").submit();
}
});
</script>
@using (Html.BeginForm())
{
<p>
@Html.DropDownList("specifiedRange", new SelectList(
ViewBag.rangeList as System.Collections.IEnumerable,
"Text",
"Value",
new { @Id = "range" }))
</p>
<p class = "down">
Starting Date: @(Html.Telerik().DateTimePicker().Name("startingDate"))
Ending Date: @(Html.Telerik().DateTimePicker().Name("endingDate"))
<input type="submit" value="GO" />
</p>
}
答案 0 :(得分:1)
是的,需要使用jquery完成。像这样:
$(document).ready(function(){
$("#idofyourdropdown").change(function(){
if($(this).val() == "Custom")
{
$("p.down").toggle();
}
else{
$("form").submit();
}
});
});
将“向下”分类到您想要展示的<p>
。
答案 1 :(得分:0)
这是你在找什么?
$('#specifiedRange').change(function() {
if($(this).val() !== 'Custom') $(this).closest('form').submit();
})