-5

在“From”字段中,用户选择一个日期,在“To”字段中,日期不应小于“From”字段的日期。

我怎样才能做到这一点?

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

        $('.ui-datepicker-prev ui-corner-all').hide();

        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: "-0:c+10",
            dateFormat: "dd - M - yy",
            onClose: function(selectedDate) {
                $('#datepicker1').datepicker("setDate", selectedDate);
            }
        });

        $('#datepicker').datepicker("setDate", new Date());

        $("#datepicker1").datepicker({
            changeMonth:false,
            yearRange: "-0:+0",
            dateFormat: "dd - M - yy"
        });
    });
</script>

这是我的代码(到目前为止):http: //jsfiddle.net/Mounika_Garrepalli/8z2UH/

4

5 回答 5

2

您可以通过将日期传递给函数来比较日期

var dt1_temp = date1.split("-");
var ctdt1 = new Date(dt1_temp[2],dt1_temp[1],dt1_temp[0]); // Y/m/d format
var dt2_temp = date2.split("-");
var ctdt2 = new Date(dt2_temp[2],dt2_temp[1],dt2_temp[0]);

现在我们可以比较日期

if(ctdt1>ctdt2) {
alert("To date should not be equal or before the from date");
}
于 2013-06-26T10:58:42.057 回答
0

尝试这个:

$(document).ready(function() {

    $('.ui-datepicker-prev ui-corner-all').hide();
    $("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "-0:c+10",
        dateFormat: "dd - M - yy",
        onSelect: function(selectedDate) {
            $("#datepicker1").datepicker("option", "minDate", selectedDate);
            $('#datepicker1').datepicker("setDate", selectedDate);
        }
    });
    $('#datepicker').datepicker("setDate", new Date());

    $("#datepicker1").datepicker({
        yearRange: "-0:+0",
        dateFormat: "dd - M - yy"
    });

});
于 2013-06-26T10:35:38.243 回答
0

一旦尝试这个

        $(document).ready(function() {

        $('.ui-datepicker-prev ui-corner-all').hide();
        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: "-0:c+10",
            dateFormat: "dd - M - yy",
            onClose: function(selectedDate) {
                $('#datepicker1').datepicker("setDate", selectedDate);
            }
        });
        $('#datepicker').datepicker("setDate", new Date());
        $("#datepicker").change(function() {
             datepicker1_mindate = $(this).datepicker('getDate');
             datepicker1_mindate = new Date(datepicker1_mindate.getTime());
             $("#datepicker1").datepicker("option", "minDate", datepicker1_mindate);
         });
        $("#datepicker1").datepicker({
            yearRange: "-0:+0",
            dateFormat: "dd - M - yy",                
        });

    });
于 2013-06-26T10:31:25.000 回答
0

这是小提琴

        $(document).ready(function () {

        $('.ui-datepicker-prev ui-corner-all').hide();
        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: "-0:c+10",
            dateFormat: "dd - M - yy",
            onClose: function (selectedDate) {
                $('#datepicker1').datepicker("setDate", selectedDate);
            }
        });
        $('#datepicker').datepicker("setDate", new Date());

        $("#datepicker1").datepicker({
            yearRange: "-0:+0",
            dateFormat: "dd - M - yy"
        });
        $('#datepicker').change(function () {
            checkDate($(this), $('#datepicker1'));
        });
        $('#datepicker1').change(function () {
            checkDate($('#datepicker'), $(this));
        });
    });

    function checkDate(date1, date2) {
        if (date1.val() > date2.val()) {
            alert('Error');
            date2.val(date1.val());
        }
    }
于 2013-06-26T10:25:49.540 回答
0

您可以使用 minDate 选项:

$("#datepicker1").datepicker({
    yearRange: "-0:+0",
    //Set the initial minDate
    minDate: $("#datepicker").datepicker("getDate"),
    dateFormat: "dd - M - yy"
});

然后做出反应:

$("#datepicker").on("change", function() {
    $("#datepicker1").datepicker("option", "minDate", $(this).datepicker("getDate"));
});

http://jsfiddle.net/8z2UH/5/

于 2013-06-26T10:27:21.947 回答