我在使用jQuery datepicker小部件时遇到了一些麻烦。此代码按预期工作,警报(tempDate)以dd-mm-yyyy格式返回日期:
$(document).ready(function() {
$( "#datepicker" ).datepicker({
//showOtherMonths: true,
//selectOtherMonths: true,
altField: "#alternate",
altFormat: "d-m-yy"
});
$( "#datepicker" ).datepicker( "option", "dateFormat", "DD d MM, yy");
$("#datepicker").change(function(){
var tempDate = $("#alternate").val();
alert(tempDate);
});
});
但是,当我添加minDate和maxDate时,alert(tempDate)不返回任何内容:
$(document).ready(function() {
$( "#datepicker" ).datepicker({ minDate: -20, maxDate: +20 });
$( "#datepicker" ).datepicker({
//showOtherMonths: true,
//selectOtherMonths: true,
altField: "#alternate",
altFormat: "d-m-yy"
});
$( "#datepicker" ).datepicker( "option", "dateFormat", "DD d MM, yy");
$("#datepicker").change(function(){
var tempDate = $("#alternate").val();
alert(tempDate);
});
});
这是HTML:
<label>Date: </label><input type="text" id="datepicker" size="30"><input type="hidden" id="alternate" />
非常感谢任何帮助。谢谢!
答案 0 :(得分:4)
您正在设置选项两次,因此您将覆盖选项,尝试将它们全部设置一次。
代码:
$("#datepicker").datepicker({
minDate: -20,
maxDate: +20,
altField: "#alternate",
altFormat: "d-m-yy",
dateFormat: "DD d MM, yy"
});