我有以下代码,并且仅在maxDate
时才想使用selector === '#Birth'
。
我该如何实现?
function customdatepicker(selector) {
$(selector).datepicker({
inline: true,
showOtherMonths: true,
dateFormat: 'dd/mm/yy',
prevText: '<',
nextText: '>',
maxDate: 0
});
}
答案 0 :(得分:0)
初始化后,可以使用option
(datepicker('option', ...)
)方法来更新选择器的最大日期。
customdatepicker('#test');
customdatepicker('#Birth');
function customdatepicker(selector) {
const picker = $(selector).datepicker({
inline: true,
showOtherMonths: true,
dateFormat: 'dd/mm/yy',
prevText: '<',
nextText: '>'
});
if (selector === '#Birth') {
// Impossible to choose a date after the 20th of May 2019.
picker.datepicker('option', 'maxDate', '20/05/2019');
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<label for="test">Date: <input type="text" id="test"></label>
<label for="Birth">Birth: <input type="text" id="Birth"></label>