如何设置asp:textbox textmode=date 的最小和最大日期???使用 javascript/jquery

问题描述 投票:0回答:4

我有一个 asp:textbox ,我如何使用 javascript 将其最小日期设置为今天:

使用 C#,我这样做并且工作正常..但我必须使用 Js/Jquery 来完成

DateTime date = DateTime.Today.Date;
            String today = date.ToString("yyyy-MM-dd");

            tourStartDate.Attributes["min"] =today;
<asp:TextBox Width="95%" ID="tourStartDate" runat="server" TextMode="Date" onchange="SetDate()"></asp:TextBox></td>
javascript jquery asp.net
4个回答
3
投票

关于 C# 背后的代码:

tourStartDate.Attributes["max"] = DateTime.Now.ToString("yyyy-MM-dd");

VB.net:

tourStartDate.Attributes("max") = Now.ToString("yyyy-MM-dd")

1
投票

您需要将

ClientIdMode = static
放入服务器控件中才能获取静态 Id。

<asp:TextBox Width="95%" ID="tourStartDate" ClientIdMode = "static" runat="server" TextMode="Date" onchange="SetDate()"></asp:TextBox>

Jquery:在属性中设置今天。

$("#tourStartDate").attr("min", (new Date()));

编辑:

你可以尝试吗

<input type="date" min="2015-07-01" max="2015-10-20">

JQuery:

  $("#tourStartDate").attr("min", (new Date()).toISOString().substring(0,10));

确保您的文档类型是 html(适用于 HTML 5 控件)

编辑2:

JavaScript :

 document.getElementById('tourStartDate').setAttribute('min', (new Date()).toISOString().substring(0,10));

0
投票

我的设计代码是:

<asp:TextBox ID="txtDate" runat="server" MaxLength="30" CssClass="form-control" autocomplete="off"></asp:TextBox>

Javascript代码和jquery代码

$(function () {
  $("#ContentPlaceHolder1_txtDate").datepicker({
    minDate: new Date(new Date().getTime() - 2 * 24 * 60 * 60 * 1000),
    maxDate: new Date()
  });
});

0
投票

虽然不是 javascript/jQuery 解决方案,但这更像是 ASP.NET 风格。使用 OnInit 将属性设置为您想要的。

ASPX:

 <asp:TextBox ID="calEnd" runat="server" CssClass="datefield" data-val="true" OnInit="cal_Init"  data-val-required="End date is required" type="date" />

背后代码:

protected void cal_Init(object sender, EventArgs e)
    {
        var inp = (TextBox)sender;
        inp.Attributes.Add("min", (DateTime.Now.Year - 10).ToString() + "-01-01");
        inp.Attributes.Add("max", (DateTime.Now.Year + 2).ToString() + "-12-31");
    }
© www.soinside.com 2019 - 2024. All rights reserved.