在ASP.NET Core项目中禁用jQuery DatePicker的过去日期

时间:2019-04-05 14:53:04

标签: c# .net asp.net-core core

我在项目中创建日期选择器时遇到麻烦。我需要禁用过去的日期。我尝试了所有我认为的方法,但没有任何效果。我还需要保留输入值。有人可以帮我吗?

我在脚本中尝试了minDate,但无法正常工作。

@model EscapeGame2.ViewModels.RoomInfoViewModel

@{
    ViewData["Title"] = "Details";
}

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
    $('#datefield').datepicker({ minDate: 0 });  
</script>

<h1>Details</h1>

 <div>
    <h4>Room</h4>
    <hr />
    <dl class="row">
    <dt class="col-sm-2">
        @Html.DisplayNameFor(model => model.Name)
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.Name)
    </dd>
    <dt class="col-sm-2">
        @Html.DisplayNameFor(model => model.Description)
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.Description)
    </dd>
    <dt class="col-sm-2">
        <input class="datefield" data-val="true" data-val-required="Date is required"
               id="ReleaseDate" name="ReleaseDate" type="date" value="1/11/1989" />
    </dt>
</dl>

2 个答案:

答案 0 :(得分:0)

    @model EscapeGame2.ViewModels.RoomInfoViewModel

@{
    ViewData["Title"] = "Details";
}

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
    $('.datepicker').datepicker({ minDate: 0 });  
</script>

<h1>Details</h1>

 <div>
    <h4>Room</h4>
    <hr />
    <dl class="row">
    <dt class="col-sm-2">
        @Html.DisplayNameFor(model => model.Name)
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.Name)
    </dd>
    <dt class="col-sm-2">
        @Html.DisplayNameFor(model => model.Description)
    </dt>
    <dd class="col-sm-10">
        @Html.DisplayFor(model => model.Description)
    </dd>
    <dt class="col-sm-2">
        <input class="datepicker" data-val="true" data-val-required="Date is required"  id="ReleaseDate" name="ReleaseDate" type="date" value="1/11/1989" />
    </dt>
</dl>

答案 1 :(得分:0)

要禁用jQuery DatePicker中的过去日期,您必须使用new Date()作为minDate的值,如下所示:

 $(function() {
    $( ".datepicker" ).datepicker({
        minDate: new Date()
    });
});