jqueryui日期选择器,日期范围限制为今年
我在过去的几个小时里一直在修补和寻找答案,但我相信这对这里的一些人来说是相当简单的 我希望在日期范围模式下使用jqueryui的Datepicker,但是我想限制只能选择今年的日期。我还想将日期格式从年月日更改为年月日 我将发布到原始代码,因为我不认为发布任何失败的努力有任何价值 希望有人能帮我,谢谢jqueryui日期选择器,日期范围限制为今年,jquery,jquery-ui,datepicker,Jquery,Jquery Ui,Datepicker,我在过去的几个小时里一直在修补和寻找答案,但我相信这对这里的一些人来说是相当简单的 我希望在日期范围模式下使用jqueryui的Datepicker,但是我想限制只能选择今年的日期。我还想将日期格式从年月日更改为年月日 我将发布到原始代码,因为我不认为发布任何失败的努力有任何价值 希望有人能帮我,谢谢 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta na
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Select a Date Range</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
var dateFormat = "mm/dd/yy",
from = $( "#from" )
.datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
})
.on( "change", function() {
to.datepicker( "option", "minDate", getDate( this ) );
}),
to = $( "#to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
})
.on( "change", function() {
from.datepicker( "option", "maxDate", getDate( this ) );
});
function getDate( element ) {
var date;
try {
date = $.datepicker.parseDate( dateFormat, element.value );
} catch( error ) {
date = null;
}
return date;
}
} );
</script>
</head>
<body>
根据您的需要调整以下代码:
$(function() {
var year = (new Date).getFullYear();
$( "#from" ).datepicker({
minDate: new Date(year, 0, 1),
maxDate: new Date(year, 11, 31)
});
});
及
原职位:及
对于您的特定示例,这可以像这样应用:未经测试我没有您的完整代码/css:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Select a Date Range</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script>
$( function() {
var year = (new Date).getFullYear();
from = $( "#from" ).datepicker({
minDate: new Date(year, 0, 1),
maxDate: new Date(year, 11, 31),
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
})
.on( "change", function() {
$( "#to" ).datepicker( "option", "minDate", new Date($("#from").datepicker().val() )); // You set the min date of `to` to be the selected date here
}),
to = $( "#to" ).datepicker({
maxDate: new Date(year, 11, 31), // minDate has already been set
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
});
function getDate( element ) {
var date;
try {
date = $(element).datepicker({ dateFormat: 'dd-mm-yy' }).val();
} catch( error ) {
date = null;
}
return date;
}
});
</script>
</head>
<body>
<!-- dummy datepickers -->
<div id="from"></div>
<div id="to"></div>
<!-- It is a good practice to import scripts at the end of your page, helps to render quicker the DOM -->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</body>
</html>
希望有帮助 谢谢你,Antoine,但是这在原始代码中是怎么回事?我需要删除任何现有的信息吗。抱歉,我可能问了一个除了我以外所有人都知道的问题@好的,我会更新我的答案。然而,我真的建议您花时间测试/理解/定制我的代码片段,而不是简单地复制/粘贴它;非常感谢你的帮助,我打算从中学习,别问别的。不客气,我只是说以防万一。快乐编码-
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Select a Date Range</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script>
$( function() {
var year = (new Date).getFullYear();
from = $( "#from" ).datepicker({
minDate: new Date(year, 0, 1),
maxDate: new Date(year, 11, 31),
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
})
.on( "change", function() {
$( "#to" ).datepicker( "option", "minDate", new Date($("#from").datepicker().val() )); // You set the min date of `to` to be the selected date here
}),
to = $( "#to" ).datepicker({
maxDate: new Date(year, 11, 31), // minDate has already been set
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3
});
function getDate( element ) {
var date;
try {
date = $(element).datepicker({ dateFormat: 'dd-mm-yy' }).val();
} catch( error ) {
date = null;
}
return date;
}
});
</script>
</head>
<body>
<!-- dummy datepickers -->
<div id="from"></div>
<div id="to"></div>
<!-- It is a good practice to import scripts at the end of your page, helps to render quicker the DOM -->
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</body>
</html>