首页 > 解决方案 > 如何在下拉列表中添加“Customdate”作为链接并在单击时打开日期?

问题描述

我想在下拉列表中添加自定义日期,然后单击它,应该在 MVC 中打开日期。

jQuery

 $("#CustomDate").datepicker({
            numberOfMonths: 1,
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
        });

看法

  @Html.DropDownListFor(m => m.Period, new List<SelectListItem>
                         {
                         ,
                          new SelectListItem {Value = "03/05/2018", Text = "Current Year" },
                          new SelectListItem {Value = "03/05/2017", Text = "Last Year" },
                          new SelectListItem {Value = "CustomDate", Text = "Custom Date"} // want to make CustomDate as link and want to open Date
                         },
                              new { @class = "form-control" })

标签: jqueryasp.net-mvcjquery-ui-datepicker

解决方案


您可以将不同的类添加到下拉列表中,附加一个 jquery 更改事件处理程序,然后在其中查看具有“CustomDate”值的元素。满足该条件时,您可以打开日期选择器。请参阅下面的工作示例:

$('.my-select').change(function(evt){
  if(this.value === 'Custom'){
    console.log('Custom Selected!');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="my-select form-control">
  <option value="1">Opt 1</option>
  <option value="2">Opt 2</option>
  <option value="Custom">Custom Date</option>
</select>


推荐阅读