我正在使用jQuery datepicker来请求来自用户的日期输入。我已经设置了本地默认值并让它生成我想要的正确日期格式(dd / mm / yyyy)。出于某种原因,此日期以表格(mm / dd / yyyy)发布 - 如何确保发布正确的格式?
设置默认值:
jQuery(function($) {
$.datepicker.regional['en-GB'] = {
monthNames: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
monthNamesShort: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
dayNames: [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
],
dayNamesShort: [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat'
],
dayNamesMin: [
'Su',
'Mo',
'Tu',
'We',
'Th',
'Fr',
'Sa'
],
dateFormat: 'dd/mm/yy',
firstDay: 1,
renderer: $.datepicker.defaultRenderer,
prevText: 'Prev',
prevStatus: 'Show the previous month',
prevJumpText: '<<',
prevJumpStatus: 'Show the previous year',
nextText: 'Next',
nextStatus: 'Show the next month',
nextJumpText: '>>',
nextJumpStatus: 'Show the next year',
currentText: 'Current',
currentStatus: 'Show the current month',
todayText: 'Today',
todayStatus: 'Show today\'s month',
clearText: 'Clear',
clearStatus: 'Erase the current date',
closeText: 'Done',
closeStatus: 'Close without change',
yearStatus: 'Show a different year',
monthStatus: 'Show a different month',
weekText: 'Wk',
weekStatus: 'Week of the year',
dayStatus: 'Select DD, M d',
defaultStatus: 'Select a date',
isRTL: false
};
$.datepicker.setDefaults($.datepicker.regional['en-GB']);
});
初始化datepicker
$(".datepicker").each(function () {
$(this).datepicker({
onSelect: onSelectHandler
});
});
答案 0 :(得分:3)
如果您正在使用UI Jquery,请在初始化时设置正确的格式,如下所示:
$(".selector").datepicker({dateFormat: 'dd/mm/yy'});
答案 1 :(得分:3)
你应该只需要这样做:
$( "#datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' });
编辑:看起来你的代码适合我。做了new fiddle with your code。此外,您不需要执行.each()。只需$(“。datepicker”)。datepicker({onSelect:onSelectHandler});应该工作。