2013-07-15 28 views
6

Tôi có một thanh công cụ ngày của giao diện người dùng Jquery như thế này.tùy chọn maxDate trong datepicker

enter image description here
Tôi chỉ có thể chọn tháng và năm từ trình ghi ngày tháng.
mã: -

$('#datepicker').datepicker({ 
          changeMonth: true, 
          changeYear: true, 
          showButtonPanel: true, 
          dateFormat: 'M yy', 
          maxDate: '0',      
          onClose: function() { 
            var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
            var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); 
            $(this).datepicker('setDate', new Date(iYear, iMonth, 1));        
          }, 
          beforeShow: function(input, inst) { 
            $(inst.dpDiv).addClass('calendar-off');        
          } 
         }); 

Nếu tôi đặt maxDate: '0' tháng tối đa và năm tôi có thể chọn là tháng hiện tại và năm hiện tại.
Tôi muốn đặt tháng và năm tối đa bằng jquery. Đối với một datepicker bình thường với ngày, tháng, năm, đầu tiên tôi loại bỏ maxDate: '0' mã này và tôi đã sử dụng đoạn mã sau để thiết lập ngày tối đa

var maximumDate = '07-15-2013'; 
$("#datepicker").datepicker("option", "maxDate", maximumDate); 

Làm thế nào tôi có thể thiết lập ngày tối đa trong tháng, năm chọn? Mã trên không hoạt động trong trường hợp này.Vui lòng cho tôi đề xuất của bạn.

+0

maxDate chuỗi phải theo định dạng xác định bởi các tùy chọn DateFormat. Kiểm tra tùy chọn dateFormat của bạn hoặc sử dụng đối tượng ngày thay thế: 'var maximumDate = new Date (2013,7,15);'. – pipo

+0

@pipo ("# datepicker") .datepicker ("tùy chọn", "maxDate", "15/7/2013"); mã này hoạt động tốt cho datepicker bình thường.Nhưng không phải trong bộ chọn tháng và năm. – Nandu

+0

@pipo Tôi đã cố gắng rất nhiều như bạn đã nói, Cuối cùng tôi đã tìm thấy câu trả lời.Cảm ơn pipo – Nandu

Trả lời

5

Đã thử mã sau. Nó hoạt động tốt cho tôi.

var date = new Date("2013-07-15"); 
    var currentMonth = date.getMonth(); 
    var currentDate = date.getDate(); 
    var currentYear = date.getFullYear(); 
    $("#datepicker").datepicker("option", "maxDate", new Date(currentYear, currentMonth, currentDate)); 

Ví dụ fiddle tôi đã tạo: FIDDLE

3

Hãy thử này để lựa chọn max/min ngày từ tháng, năm datepicker.

Xem fiddle này cho các giải pháp đầy đủ: Month/Year Picker @ JSFiddle

var searchMinDate = "-2y"; 
var searchMaxDate = "-1m"; 
if ((new Date()).getDate() <= 5) { 
    searchMaxDate = "-2m"; 
} 
$("#txtFrom").datepicker({ 
    dateFormat: "M yy", 
    changeMonth: true, 
    changeYear: true, 
    showButtonPanel: true, 
    showAnim: "", 
    minDate: searchMinDate, 
    maxDate: searchMaxDate, 
    showButtonPanel: true, 
    beforeShow: function (input, inst) { 
     if ((datestr = $("#txtFrom").val()).length > 0) { 
      var year = datestr.substring(datestr.length - 4, datestr.length); 
      var month = jQuery.inArray(datestr.substring(0, datestr.length - 5), "#txtFrom").datepicker('option', 'monthNamesShort')); 
     $("#txtFrom").datepicker('option', 'defaultDate', new Date(year, month, 1)); 
       $("#txtFrom").datepicker('setDate', new Date(year, month, 1)); 
      } 
     }, 
     onClose: function (input, inst) { 
      var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
      var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); 
      $("#txtFrom").datepicker('option', 'defaultDate', new Date(year, month, 1)); 
      $("#txtFrom").datepicker('setDate', new Date(year, month, 1)); 
      var to = $("#txtTo").val(); 
      $("#txtTo").datepicker('option', 'minDate', new Date(year, month, 1)); 
      if (to.length > 0) { 
       var toyear = to.substring(to.length - 4, to.length); 
       var tomonth = jQuery.inArray(to.substring(0, to.length - 5), $("#txtTo").datepicker('option', 'monthNamesShort')); 
       $("#txtTo").datepicker('option', 'defaultDate', new Date(toyear, tomonth, 1)); 
       $("#txtTo").datepicker('setDate', new Date(toyear, tomonth, 1)); 
      } 
     } 
    }); 
    $("#txtTo").datepicker({ 
     dateFormat: "M yy", 
     changeMonth: true, 
     changeYear: true, 
     showButtonPanel: true, 
     showAnim: "", 
     minDate: searchMinDate, 
     maxDate: searchMaxDate, 
     showButtonPanel: true, 
     beforeShow: function (input, inst) { 
      if ((datestr = $("#txtTo").val()).length > 0) { 
       var year = datestr.substring(datestr.length - 4, datestr.length); 
       var month = jQuery.inArray(datestr.substring(0, datestr.length - 5), $("#txtTo").datepicker('option', 'monthNamesShort')); 
       $("#txtTo").datepicker('option', 'defaultDate', new Date(year, month, 1)); 
       $("#txtTo").datepicker('setDate', new Date(year, month, 1)); 
      } 
     }, 
     onClose: function (input, inst) { 
      var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
      var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); 
      $("#txtTo").datepicker('option', 'defaultDate', new Date(year, month, 1)); 
      $("#txtTo").datepicker('setDate', new Date(year, month, 1)); 
      var from = $("#txtFrom").val(); 
      $("#txtFrom").datepicker('option', 'maxDate', new Date(year, month, 1)); 
      if (from.length > 0) { 
       var fryear = from.substring(from.length - 4, from.length); 
       var frmonth = jQuery.inArray(from.substring(0, from.length - 5), $("#txtFrom").datepicker('option', 'monthNamesShort')); 
       $("#txtFrom").datepicker('option', 'defaultDate', new Date(fryear, frmonth, 1)); 
       $("#txtFrom").datepicker('setDate', new Date(fryear, frmonth, 1)); 
      } 

     } 
    }); 

Cũng thêm video này vào một khối phong cách như đã đề cập ở trên:

.ui-datepicker-calendar { display: none !important; } 
Các vấn đề liên quan