2017-08-01 90 views
-2

我使用Bootstrap Datepicker如何設置限制以僅使用multidates選項來選擇最多3個日期。Bootstrap Datepicker:如何設置限制以僅選擇3個日期?

var todayDate = moment().format('mm-dd-yyyy'); 
dp = $("#leaveDatePicker").datepicker({ 
    format    : "mm-dd-yyyy", 
    multidate   : true, 
    inline    : true, 
    todayHighlight  : false, 
    daysOfWeekDisabled : [0], 
    startDate   : 'today', 
    beforeShowDay  : function(date){ 
     var d   = date; 
     var curr_month = d.getMonth() + 1; //Months are zero based 
     if(curr_month < 10) 
      curr_month = '0'+curr_month; 
     var formattedDate = curr_month + "-" + d.getDate() + "-" +d.getFullYear() 
     if ($.inArray(formattedDate, myActiveDates) != -1){     
      return { 
       classes: 'active' 
      }; 
     } 
     return [true,""]; 
    } 
}); 
dp.data('datepicker').setDates($('input#datestring').val().split(',')); 
dp.on('changeDate', function (e){ 
    $('input#datestring').val($(this).data('datepicker').getFormattedDate()); 
}); 
+1

請把你的一些代碼 –

+0

我怎麼粘貼我的代碼?它顯示消息**評論太長**實際上我新來這裏 –

+0

編輯您的問題,並添加代碼 – amicoderozer

回答

0

使用變量來存儲選定的日期數組。

每當日期被選擇檢查在日期選擇器中的數據的長度,並且如果它大於3做從所存儲陣列的復位,並通知用戶

var selectedDates = []; 
dp.on('changeDate', function(e) { 

    if (e.dates.length < 4) { 
    // store current selections 
    selectedDates = e.dates 
    } else { 
    // reset dates if 4th selected 
    dp.data('datepicker').setDates(selectedDates); 
    alert('Can only select 3 dates') 
    } 

}); 

DEMO

相關問題