在“发件人”字段中,用户选择一个日期,在“收件人”字段中,日期不应小于“发件人”字段日期。
我怎样才能做到这一点?
<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/
答案 0 :(得分: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");
}
答案 1 :(得分:0)
这是 FIDDLE
$(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());
}
}
答案 2 :(得分: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"));
});
答案 3 :(得分:0)
尝试this
$(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",
});
});
答案 4 :(得分: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"
});
});