如何阻止用户选择开始时间之前的结束时间

时间:2013-06-26 20:04:34

标签: javascript jquery jquery-ui jquery-ui-timepicker

我使用datetimepicker在表单中设置值。我一直试图限制用户选择开始时间之前的结束时间。我已经尝试设置mindate,hourmin,一个等于开始日期的新日期,但没有成功。根据我的看法,以下代码应该可以工作,但事实并非如此。

        <script src="../js/jquery-1.9.1.js"></script>
    <script src="../js/jquery-ui-1.10.3.custom.js"></script>
    <script src="../js/jquery-ui-timepicker-addon.js"></script>
    <script src="../js/jquery-ui-sliderAccess.js"></script>
    <script>

        $(function() 
            {

                var startDateTextBox = $('#startDate');
                var endDateTextBox = $('#endDate');
                var startTimeTextBox = $('#startTime');
                var endTimeTextBox = $('#endTime');
                startDateTextBox.datepicker(
                    {
                        minDate: ("setDate", '',new Date()),
                        hourGrid: 12,
                        minuteGrid: 15,
                        stepMinute: 15,

                        onClose: function(dateText, inst) {
                        if (endDateTextBox.val() != '') 
                        {

                            var testStartDate = startDateTextBox.datetimepicker('getDate');
                            var testEndDate = endDateTextBox.datetimepicker('getDate');
                            if (testStartDate > testEndDate)
                            endDateTextBox.datetimepicker('setDate', testStartDate);

                        }
                        else 
                        {
                            endDateTextBox.val(dateText);
                        }
                        },
                        onSelect: function (selectedDateTime){
                        endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
                        }
                    }
                );
                startTimeTextBox.datetimepicker(
                {
                    timeOnly:true,
                    minuteGrid: 15,
                    stepMinute: 15,
                    minDate: ("setTime", '',new Date()),
                    timeFormat: 'hh:mm tt',
                    onClose: function(dateText, inst)
                    {
                        if (endTimeTextBox.val() != '') 
                        {
                        var testStartTime = startTimeTextBox.datetimepicker('getDate');
                        var testEndTime = endTimeTextBox.datetimepicker('getDate');
                        if(testStartTime > testEndTime)
                        endTimeTextBox.timepicker('setTime', testStartTime);
                        }
                        else
                        {
                            endTimeTextBox.val(dateText)
                        }
                    },
                    onSelect: function (selectedDateTime){
                    endTimeTextBox.datetimepicker('setDate', startTimeTextBox.datetimepicker('getDate') );
                    }
                }
                );
                endDateTextBox.datepicker(
                    {
                        hourGrid: 12,
                        minuteGrid: 15,
                        stepMinute: 15,

                        onClose: function(dateText, inst) 
                        {
                            if (startDateTextBox.val() != '') 
                            {
                                var testStartDate = startDateTextBox.datetimepicker('getDate');
                                var testEndDate = endDateTextBox.datetimepicker('getDate');
                                if (testStartDate > testEndDate)
                                startDateTextBox.datetimepicker('setDate', testEndDate);
                            }
                            else 
                            {
                                startDateTextBox.val(dateText);
                            }
                        },
                        onSelect: function (selectedDateTime){
                        startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
                        }
                    }
                );
                endTimeTextBox.datetimepicker(
                {
                    timeOnly:true,
                    minuteGrid: 15,
                    stepMinute: 15,
                    timeFormat: 'hh:mm tt',

                    onClose: function(dateText, inst) 
                    {
                        if (startTimeTextBox.val() != '') 
                        {
                            var testStartTime = startTimeTextBox.datetimepicker('getDate');
                            var testEndTime = endTimeTextBox.datetimepicker('getDate');
                            if (testStartTime > testEndTime)
                            endTimeTextBox.datetimepicker('setDate', testStartTime);
                        }
                        else 
                        {
                            startTimeTextBox.val(dateText);
                        }
                    },
                    onSelect: function (selectedDateTime){
                    startTimeTextBox.datetimepicker('option', 'maxDate', endTimeTextBox.datetimepicker('getDate') );
                    }
                }
                );
            }
        );
    </script>

代码的逻辑似乎是正确的,为什么它不起作用? 到目前为止我想出的是:

            $(function() 
        {
            var startDateTextBox = $('#startDate');
            var endDateTextBox = $('#endDate');
            startDateTextBox.datetimepicker(
                {
                    altField:"#startTimeALT",
                    altFieldTimeOnly: true,
                    timeFormat: "h:mm tt",
                    minDate: ("setDate", '',new Date()),
                    hourGrid: 12,
                    minuteGrid: 15,
                    stepMinute: 15,

                    onClose: function(dateText, inst) 
                    {
                        if (endDateTextBox.val() != '') 
                        {
                            var testStartDate = startDateTextBox.datetimepicker('getDate');
                            var testEndDate = endDateTextBox.datetimepicker('getDate');
                            if (testStartDate > testEndDate)
                            endDateTextBox.datetimepicker('setDate', testStartDate);
                        }
                        else 
                        {
                            endDateTextBox.val(dateText);
                        }
                        },
                        onSelect: function (selectedDateTime){
                        endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
                    }
                }
                );
                endDateTextBox.datetimepicker(
                    {
                        minDate: ("setDate", '',new Date()),
                        altField:"#endTimeALT",
                        altFieldTimeOnly: true,
                        timeFormat: "h:mm tt",
                        hourGrid: 12,
                        minuteGrid: 15,
                        stepMinute: 15,

                        onClose: function(dateText, inst) 
                        {
                            if (startDateTextBox.val() != '') 
                            {
                                var testStartDate = startDateTextBox.datetimepicker('getDate');
                                var testEndDate = endDateTextBox.datetimepicker('getDate');
                                if (testStartDate > testEndDate)
                                startDateTextBox.datetimepicker('setDate', testEndDate);
                            }
                            else 
                            {
                                startDateTextBox.val(dateText);
                            }
                        },
                        onSelect: function (selectedDateTime){
                        startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
                        }
                    }
                );
            }
        );

在上面的代码中,我使用datetimepicker jquery插件分隔了日期和时间字段。我这样做是为了更好地控制输入以及如何将其存储在数据库中。我希望这将有助于我的查询,并允许更容易的搜索和更新功能。这是用PHP编写的调度项目的一部分,jquery支持用户交互。非常感谢您在此努力中提供的任何帮助和建议!

1 个答案:

答案 0 :(得分:2)

这个想法是正确的,问题是如何比较你的日期。我试过调用datepicker("getDate"),它返回一个String,我假设你的datetimepicker插件是相同的。

您比较的字符串看起来像Wed Jun 12 2013 00:00:00 GMT+0200 (CEST),这里的等式只是基于Weekdate的字典顺序。

但是如果你使用JavaScript方法Date.parse(),你可以轻松地比较它们:

var testStartDate = startDateTextBox.datetimepicker('getDate');
var testEndDate = endDateTextBox.datetimepicker('getDate');
if (Date.parse(testStartDate) > Date.parse(testEndDate))
  endDateTextBox.datetimepicker('setDate', testStartDate);

有了这个要点,我有一个玩具网站,你可以尝试验证,第一个日期已经有效:https://gist.github.com/contradictioned/5871967