我有2个文本框,其中包含From date和To date以及ASP Submit按钮,如下所示
<div id="validation"></div>
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MM/yyyy" TargetControlID="FromDateTxtBox" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" Format="dd/MM/yyyy" TargetControlID="ToDateTextBox" />
From Date:<asp:TextBox ID="FromDateTxtBox" runat="server"></asp:TextBox>
To Date:<asp:TextBox ID="ToDateTextBox" runat="server"></asp:TextBox>
<asp:Button ID="SubmitButton" runat="server" Text="Button"/>
现在使用jQuery我必须比较2个日期:
$("#<%=SubmitButton.ClientID %>").click(function () {
var Fromdate = Date.parse($("#<%=FromDateTxtBox.ClientID %>").val());
var Todate = Date.parse($("#<%=ToDateTextBox.ClientID %>").val());
if (Fromdate <= Todate) {
$("#validation").text("is less than");
return false;
} else {
$("#validation").text("greater than");
return false;
}
});
然而,每次重复大于。这有什么不对?我的目标是,如果From Date大于To Date,它应该给出错误消息。
答案 0 :(得分:0)
$("#<%=SubmitButton.ClientID %>").click(function () {
debugger;
var date =$("#<%=FromDateTxtBox.ClientID %>").val();
var dates = $("#<%=ToDateTextBox.ClientID %>").val();
var d = new Date(date.split("/").reverse().join("-"));
var dd = d.getDate();
var mm = d.getMonth() + 1;
var yy = d.getFullYear();
var newdate = yy + "/" + mm + "/" + dd;
var ds = new Date(dates.split("/").reverse().join("-"));
var dds = ds.getDate();
var mms = ds.getMonth() + 1;
var yys = ds.getFullYear();
var newdates = yys + "/" + mms + "/" + dds;
if (newdate <= newdates) {
$("#validation").text("is less than");
return false;
}
else
{
$("#validation").text("greater than");
return false;
}
});
转换为正确的格式并进行比较。这对我有用。