jQuery UI 用于选择年月的 DatePicker 不工作
jQuery UI DatePicker for month and year selection is not working
我正在使用 jquery datepicker 作为 monthpicker 它正在工作,但唯一的问题是如果我 select 从 calander 一个月然后它在输入字段中显示那个月份,但是当我点击再次在该输入字段上,它不会显示 selected 月份,但会显示当前月份。
HTML
<label for="startDate">Date :</label>
<input name="startDate" id="startDate" class="date-picker" />
JS
$(function() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) {
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
console.log('Done is pressed')
}
}
});
});
这是我的问题 fiddle。
http://jsfiddle.net/DBpJe/5103/
有一种方法可以通过将 currentdate 设置为 datetimepicker
$("#datepicker").datepicker("setDate", currentDate);
这是工作示例 JQFAQ Topic。
像那样尝试
$('#startDate').datepicker({
dateFormat: 'mm/yy'
});
编辑:
我现在明白你说的话了。 ^
也是如此
您必须像下面那样更改 beforeShow,而且由于月份名称在字符串中,您必须有一个这样的数组来将月份映射到数字
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
beforeShow: function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
datestr = datestr.split(" ");
year = datestr[1];
month = monthNames.indexOf(datestr[0]);
$(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
$(this).datepicker('setDate', new Date(year, month, 1));
$(".ui-datepicker-calendar").hide();
}
}
这里是demo 1
或者您可以使用这种更好看的方法将月份转换为数字
function getMonthFromString(mon){
return new Date(Date.parse(mon +" 1, 2012")).getMonth()+1
}
礼貌:SO answer
这里是demo 2
同时增加了一个新功能:限制to date晚于from date,
<script type="text/javascript">
$(function() {
$( "#from, #to").datepicker(
{
dateFormat: "yy/mm",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showOn: "button",
buttonImage: "../../static/calendar.gif",
buttonImageOnly: true,
//buttonText: "Select date",
onClose: function(dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
$('.from').focusout()//Added to remove focus from datepicker input box on selecting date
}
},
beforeShow : function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNames'));
$(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
$(this).datepicker('setDate', new Date(year, month, 1));
}
var other = this.id == "from" ? "#to" : "#from";
var option = this.id == "from" ? "maxDate" : "minDate";
if ((selectedDate = $(other).val()).length > 0) {
year = selectedDate.substring(selectedDate.length-4, selectedDate.length);
month = jQuery.inArray(selectedDate.substring(0, selectedDate.length-5), $(this).datepicker('option', 'monthNames'));
$(this).datepicker( "option", option, new Date(year, month, 1));
}
}
});
$("#btnShow").click(function(){
if ($("#from").val().length == 0 || $("#to").val().length == 0){
alert('All fields are required');
}
else{
alert('Selected Month Range :'+ $("#from").val() + ' to ' + $("#to").val());
}
}),
<!--reset-->
$(".reset").click(function() {
$(this).closest('form')[0].reset()
});
});
</script>
我正在使用 jquery datepicker 作为 monthpicker 它正在工作,但唯一的问题是如果我 select 从 calander 一个月然后它在输入字段中显示那个月份,但是当我点击再次在该输入字段上,它不会显示 selected 月份,但会显示当前月份。
HTML
<label for="startDate">Date :</label>
<input name="startDate" id="startDate" class="date-picker" />
JS
$(function() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) {
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
console.log('Done is pressed')
}
}
});
});
这是我的问题 fiddle。 http://jsfiddle.net/DBpJe/5103/
有一种方法可以通过将 currentdate 设置为 datetimepicker
$("#datepicker").datepicker("setDate", currentDate);
这是工作示例 JQFAQ Topic。
像那样尝试
$('#startDate').datepicker({
dateFormat: 'mm/yy'
});
编辑: 我现在明白你说的话了。 ^
也是如此您必须像下面那样更改 beforeShow,而且由于月份名称在字符串中,您必须有一个这样的数组来将月份映射到数字
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
beforeShow: function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
datestr = datestr.split(" ");
year = datestr[1];
month = monthNames.indexOf(datestr[0]);
$(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
$(this).datepicker('setDate', new Date(year, month, 1));
$(".ui-datepicker-calendar").hide();
}
}
这里是demo 1
或者您可以使用这种更好看的方法将月份转换为数字
function getMonthFromString(mon){
return new Date(Date.parse(mon +" 1, 2012")).getMonth()+1
}
礼貌:SO answer
这里是demo 2
同时增加了一个新功能:限制to date晚于from date,
<script type="text/javascript">
$(function() {
$( "#from, #to").datepicker(
{
dateFormat: "yy/mm",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showOn: "button",
buttonImage: "../../static/calendar.gif",
buttonImageOnly: true,
//buttonText: "Select date",
onClose: function(dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
$('.from').focusout()//Added to remove focus from datepicker input box on selecting date
}
},
beforeShow : function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNames'));
$(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
$(this).datepicker('setDate', new Date(year, month, 1));
}
var other = this.id == "from" ? "#to" : "#from";
var option = this.id == "from" ? "maxDate" : "minDate";
if ((selectedDate = $(other).val()).length > 0) {
year = selectedDate.substring(selectedDate.length-4, selectedDate.length);
month = jQuery.inArray(selectedDate.substring(0, selectedDate.length-5), $(this).datepicker('option', 'monthNames'));
$(this).datepicker( "option", option, new Date(year, month, 1));
}
}
});
$("#btnShow").click(function(){
if ($("#from").val().length == 0 || $("#to").val().length == 0){
alert('All fields are required');
}
else{
alert('Selected Month Range :'+ $("#from").val() + ' to ' + $("#to").val());
}
}),
<!--reset-->
$(".reset").click(function() {
$(this).closest('form')[0].reset()
});
});
</script>