0

我有 4 个下拉菜单 - 从和到月份选择;from 和 to 用于年份选择。在我的 javascript 函数中,如果我在两个下拉菜单中选择 From month 大于 To month of the same year,我需要显示一个警报。(例如 2013 年 7 月 - 2013 年 4 月,这在逻辑上是不正确的)。

以下是我的代码。

var pStartMonth = $('#<%= cboMonth1.ClientID %>').val();
var pStartYear = $('#<%= cboYear1.ClientID%>').val();
var pEndMonth = $('#<%= cboMonth2.ClientID%>').val();
var pEndYear = $('#<%= cboYear2.ClientID%>').val();

if ((pStartMonth > pEndMonth) && (pStartYear == pEndYear)) {
   arguments.IsValid = true;
   alert("Preferred Start Month should be lesser than Preferred End Month");
}

我的问题是,(假设我选择July 2013 - April 2013)直到执行进入 IF 循环,pStartMonth的值为 7,pEndMonth为 4。它越过这条线的那一刻,From 和 To Momths 的值都是 7 . 问题出在哪里?

编辑

我的 HTML 代码。

<asp:DropDownList ID="cboMonth1" runat="server" AutoPostBack="true" onclick="javascript:shouldsubmit=false;" ValidationGroup="vTimeSlot">
                                <asp:ListItem Value="0">-Select-</asp:ListItem>
                                <asp:ListItem Value="1">January</asp:ListItem>
                                <asp:ListItem Value="2">February</asp:ListItem>
                                <asp:ListItem Value="3">March</asp:ListItem>
                                <asp:ListItem Value="4">April</asp:ListItem>
                                <asp:ListItem Value="5">May</asp:ListItem>
                                <asp:ListItem Value="6">June</asp:ListItem>
                                <asp:ListItem Value="7">July</asp:ListItem>
                                <asp:ListItem Value="8">August</asp:ListItem>
                                <asp:ListItem Value="9">September</asp:ListItem>
                                <asp:ListItem Value="10">October</asp:ListItem>
                                <asp:ListItem Value="11">November</asp:ListItem>
                                <asp:ListItem Value="12">December</asp:ListItem>
                            </asp:DropDownList>
                            <font color="red">*</font>
                            <asp:RequiredFieldValidator ID="cboMonth1_RequiredFieldValidator" runat="server"
                                ErrorMessage="Start Month Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboMonth1" ValidationGroup="vTimeSlot" Display="None"></asp:RequiredFieldValidator>

                            <asp:DropDownList ID="cboYear1" runat="server" AutoPostBack="true" onclick="javascript:shouldsubmit=false;">
                                <asp:ListItem Value="0">-Select-</asp:ListItem>
                                <asp:ListItem Value="2013">2013</asp:ListItem>
                                <asp:ListItem Value="2014">2014</asp:ListItem>
                                <asp:ListItem Value="2015">2015</asp:ListItem>
                                <asp:ListItem Value="2016">2016</asp:ListItem>
                                <asp:ListItem Value="2017">2017</asp:ListItem>
                                <asp:ListItem Value="2018">2018</asp:ListItem>
                                <asp:ListItem Value="2019">2019</asp:ListItem>
                                <asp:ListItem Value="2020">2020</asp:ListItem>
                                <asp:ListItem Value="2021">2021</asp:ListItem>
                                <asp:ListItem Value="2022">2022</asp:ListItem>
                                <asp:ListItem Value="2023">2023</asp:ListItem>
                                <asp:ListItem Value="2024">2024</asp:ListItem>
                            </asp:DropDownList>
                            <font color="red">*</font>
                            <asp:RequiredFieldValidator ID="cboYear1_RequiredFieldValidator" runat="server"
                                ErrorMessage="Start Year Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboYear1" ValidationGroup="vTimeSlot" Display="None"></asp:RequiredFieldValidator>

<asp:DropDownList ID="cboMonth2" runat="server" AutoPostBack="true"     onclick="javascript:shouldsubmit=false;">
                                <asp:ListItem Value="0">-Select-</asp:ListItem>
                                <asp:ListItem Value="1">January</asp:ListItem>
                                <asp:ListItem Value="2">February</asp:ListItem>
                                <asp:ListItem Value="3">March</asp:ListItem>
                                <asp:ListItem Value="4">April</asp:ListItem>
                                <asp:ListItem Value="5">May</asp:ListItem>
                                <asp:ListItem Value="6">June</asp:ListItem>
                                <asp:ListItem Value="7">July</asp:ListItem>
                                <asp:ListItem Value="8">August</asp:ListItem>
                                <asp:ListItem Value="9">September</asp:ListItem>
                                <asp:ListItem Value="10">October</asp:ListItem>
                                <asp:ListItem Value="11">November</asp:ListItem>
                                <asp:ListItem Value="12">December</asp:ListItem>
                            </asp:DropDownList>
                            <font color="red">*</font>
                            <asp:RequiredFieldValidator ID="cboMonth2_RequiredFieldValidator" runat="server"
                                ErrorMessage="End Month Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboMonth2" ValidationGroup="vTimeSlot" Display="None"></asp:RequiredFieldValidator>

                            <asp:DropDownList ID="cboYear2" runat="server" AutoPostBack="true" ValidationGroup="vTimeSlot" >
                                <asp:ListItem Value="0">-Select-</asp:ListItem>
                                <asp:ListItem Value="2013">2013</asp:ListItem>
                                <asp:ListItem Value="2014">2014</asp:ListItem>
                                <asp:ListItem Value="2015">2015</asp:ListItem>
                                <asp:ListItem Value="2016">2016</asp:ListItem>
                                <asp:ListItem Value="2017">2017</asp:ListItem>
                                <asp:ListItem Value="2018">2018</asp:ListItem>
                                <asp:ListItem Value="2019">2019</asp:ListItem>
                                <asp:ListItem Value="2020">2020</asp:ListItem>
                                <asp:ListItem Value="2021">2021</asp:ListItem>
                                <asp:ListItem Value="2022">2022</asp:ListItem>
                                <asp:ListItem Value="2023">2023</asp:ListItem>
                                <asp:ListItem Value="2024">2024</asp:ListItem>
                            </asp:DropDownList>
4

1 回答 1

0

这就是我解决这个问题的方法:

// Your variable assignments
var pStartMonth = $('#<%= cboMonth1.ClientID %>').val(),
    pStartYear = $('#<%= cboYear1.ClientID%>').val(),
    pEndMonth = $('#<%= cboMonth2.ClientID%>').val(),
    pEndYear = $('#<%= cboYear2.ClientID%>').val();

// Strongly typed date objects initialised to epoch
var startDate = new Date(0),
    endDate = new Date(0);

// Set the individual components that we have to play with
startDate.setFullYear(pStartYear);
startDate.setMonth(pStartMonth);
endDate.setFullYear(pEndYear);
endDate.setMonth(pEndMonth);

// Do the comparison using the date objects
if (startDate > endDate) {
    // Handle invalid case
}

希望这可以帮助!

于 2013-10-09T10:14:38.977 回答