Jquery Datepick 范围 & asp.net 中的日期验证无效

发布于 2025-01-02 04:59:48 字数 821 浏览 4 评论 0原文

我正在使用 datepicker 在 asp.net 的 datalist 控件中选择日期。场景是用户选择2个文本框的日期,然后单击导出到excel按钮,然后系统生成包含给定日期内的数据的excel文件。问题是我想限制用户选择之前的日期单击导出按钮,选择的第二个日期应大于使用 Jquery 选择的第一个日期,以下是代码:

<asp:Button runat="server" ID="btnExportbwDates" 
    Text="Export between Dates" onclick="btnExportbwDates_Click" />
    <asp:TextBox runat="server" ID="txtDateRangeOne" CssClass="txtDateRangeOne"></asp:TextBox>
    <asp:TextBox runat="server" ID="txtDateRangeTwo" CssClass="txtDateRangeTwo"></asp:TextBox>

     <script type="text/javascript">
    $(document).ready(function () {

        var pickerOpts = {
            dateFormat: "dd/mm/yy"

        };

        $(".txtDateRangeOne").datepicker(pickerOpts);
        $(".txtDateRangeTwo").datepicker(pickerOpts);

    });
</script>

I am using datepicker to select date in datalist control in asp.net. The scenario is the user selects the date for 2 textboxes and then click on the export to excel button , then the system generates the excel file with the data within the given dates.The issue is I want to restrict the user to select the date before clicking on export button, and the 2nd date selected should be greater than the first date selected using Jquery , below is the code:

<asp:Button runat="server" ID="btnExportbwDates" 
    Text="Export between Dates" onclick="btnExportbwDates_Click" />
    <asp:TextBox runat="server" ID="txtDateRangeOne" CssClass="txtDateRangeOne"></asp:TextBox>
    <asp:TextBox runat="server" ID="txtDateRangeTwo" CssClass="txtDateRangeTwo"></asp:TextBox>

     <script type="text/javascript">
    $(document).ready(function () {

        var pickerOpts = {
            dateFormat: "dd/mm/yy"

        };

        $(".txtDateRangeOne").datepicker(pickerOpts);
        $(".txtDateRangeTwo").datepicker(pickerOpts);

    });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2025-01-09 04:59:48
    $(document).ready(function () {
            var dates = $(".txtDateRangeOne, .txtDateRangeTwo").datepicker({
                showOtherMonths: true,
                selectOtherMonths: true,
                showOn: "both",
                showAnim: "slide",
                showButtonPanel: true,
                changeMonth: true,
                changeYear: true,
                numberOfMonths: 2,
                buttonImageOnly: true,
                onSelect: function (selectedDate) {
                    var option = this.className.indexOf("txtDateRangeOne") >= 0 ? "minDate" : "maxDate";
                    instance = $(this).data("datepicker"),
                        date = $.datepicker.parseDate(
                            instance.settings.dateFormat ||
                            $.datepicker._defaults.dateFormat,
                            selectedDate, instance.settings);
                    dates.not(this).datepicker("option", option, date);
                }
            });
});
    $(document).ready(function () {
            var dates = $(".txtDateRangeOne, .txtDateRangeTwo").datepicker({
                showOtherMonths: true,
                selectOtherMonths: true,
                showOn: "both",
                showAnim: "slide",
                showButtonPanel: true,
                changeMonth: true,
                changeYear: true,
                numberOfMonths: 2,
                buttonImageOnly: true,
                onSelect: function (selectedDate) {
                    var option = this.className.indexOf("txtDateRangeOne") >= 0 ? "minDate" : "maxDate";
                    instance = $(this).data("datepicker"),
                        date = $.datepicker.parseDate(
                            instance.settings.dateFormat ||
                            $.datepicker._defaults.dateFormat,
                            selectedDate, instance.settings);
                    dates.not(this).datepicker("option", option, date);
                }
            });
});
屌丝范 2025-01-09 04:59:48

试试这个。

$(document).ready(function () {
    $(".txtDateRangeOne").datepicker({
        dateFormat: "dd/mm/yy"
        onSelect: function(date){
             //Once you select first date set this date as  the minDate 
             //of second datepicker 
             $(".txtDateRangeTwo" )
             .datepicker({ minDate: new Date(date) });
        }  
    });
    $(".txtDateRangeTwo").datepicker({ dateFormat: "dd/mm/yy" });

    $('#btnExportbwDates').click(function(){
        var startDate = $('.txtDateRangeOne').datepicker("getDate");
        var endDate = $('.txtDateRangeTwo').datepicker("getDate");
        if(!startDate){
            alert('Select start date');
            return false;
        }
        if(!endDate){
            alert('Select end date');
            return false;
        }
        if(startDate > endDate){
            alert('Select valid date range');
            return false;
        }

        return true;
    });
});

Try this.

$(document).ready(function () {
    $(".txtDateRangeOne").datepicker({
        dateFormat: "dd/mm/yy"
        onSelect: function(date){
             //Once you select first date set this date as  the minDate 
             //of second datepicker 
             $(".txtDateRangeTwo" )
             .datepicker({ minDate: new Date(date) });
        }  
    });
    $(".txtDateRangeTwo").datepicker({ dateFormat: "dd/mm/yy" });

    $('#btnExportbwDates').click(function(){
        var startDate = $('.txtDateRangeOne').datepicker("getDate");
        var endDate = $('.txtDateRangeTwo').datepicker("getDate");
        if(!startDate){
            alert('Select start date');
            return false;
        }
        if(!endDate){
            alert('Select end date');
            return false;
        }
        if(startDate > endDate){
            alert('Select valid date range');
            return false;
        }

        return true;
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文