在javascript

时间:2015-12-25 10:20:06

标签: javascript php jquery

我有javascript功能

$(document).ready(function () {

var d = new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
today = monthNames[d.getMonth()] + ' ' + d.getDate() + ' ' + d.getFullYear();

$('#to').attr('disabled', 'disabled');
$('#from').datepicker({
    defaultDate: "+1d",
    beforeShowDay: DisableSpecificDates,
    minDate: "+1d",
    maxDate: "+3M",
    dateFormat: 'dd.mm.yy',
    showOtherMonths: true,
    changeMonth: true,
    selectOtherMonths: true,
    required: true,
    altField: '#checkin',
    numberOfMonths: 1,
});

$('#from').change(function () {
    var from = $('#from').datepicker('getDate');
    var date_diff = Math.ceil((from.getTime() - Date.parse(today)) / 86400000);
    var maxDate_d = date_diff+7+'m';
    date_diff = date_diff + 1;
    $('#to').val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
        dateFormat: 'dd.mm.yy',
        minDate: date_diff,
        maxDate: maxDate_d,
        altField: '#checkout',
    });
});

$('#to').keyup(function () {
    $(this).val('');
    alert('Please select date from Calendar');
});
$('#from').keyup(function () {
    $('#from,#to').val('');
    $('#to').attr('disabled', 'disabled');
    alert('Please select date from Calendar');
});

这允许我在表单中创建JavaScript日期选择(签入和签出日期)

功能完美,但需要集成一个附加功能。

我希望禁用特定日期 - 我找到了这个JavaScript并希望集成上面的函数。

     var dakavebuli = 1000*60*60*24;
boloshesvla = new Date('2015-12-24');
bologasvla =  new Date("2015-12-25");
var shualeduritarigebi = (bologasvla.getTime()-          boloshesvla.getTime())/dakavebuli;


for(var i=0;i<=shualeduritarigebi; i++)
{
   var checking = boloshesvla.getTime()+dakavebuli*i;
   var checkout = new Date(checking);

   alert (checkout.getFullYear()+"-"+(checkout.getMonth()+1)+"-"+checkout.getDate());
}

2 个答案:

答案 0 :(得分:1)

您正在使用的功能不会向datepicker返回任何内容,因此不会阻止任何此类日期,您可以使用此功能:

这些是存储在要阻止的数组中的日期。

var $myBadDates = new Array("10 October 2010","21 November 2010","12 December 2010","13 January 2011","14 February 2011","15 March 2011");

功能定义:

function DisableSpecificDates(mydate){
var $return=true;
var $returnclass ="available";
$checkdate = $.datepicker.formatDate('dd MM yy', mydate);
for(var i = 0; i < $myBadDates.length; i++)
    {    
       if($myBadDates[i] == $checkdate)
          {
        $return = false;
        $returnclass= "unavailable";
        }
    }
return [$return,$returnclass];
}

答案 1 :(得分:0)

这是此问题的解决方案

$(document).ready(function () {

var d = new Date();

var unavailableDates = [<?=$disabledays?>];

function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == -1) {
    return [true, ""];
} else {
    return [false, "", "Unavailable"];
}
}


var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
today = monthNames[d.getMonth()] + ' ' + d.getDate() + ' ' + d.getFullYear();

$('#to').attr('disabled', 'disabled');
$('#from').datepicker({
    defaultDate: "+1d",
    beforeShowDay: unavailable,
    minDate: "+1d",
    maxDate: "+1M",
    dateFormat: 'dd.mm.yy',
    showOtherMonths: true,
    changeMonth: true,
    selectOtherMonths: true,
    required: true,
    altField: '#checkin',
    numberOfMonths: 1,
});

$('#from').change(function () {
    var from = $('#from').datepicker('getDate');
    var date_diff = Math.ceil((from.getTime() - Date.parse(today)) / 86400000);
    var maxDate_d = date_diff+7+'m';
    date_diff = date_diff + 1;
    $('#to').val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
        dateFormat: 'dd.mm.yy',
        minDate: date_diff,
        maxDate: maxDate_d,
        beforeShowDay: unavailable,
        altField: '#checkout',
    });
});

$('#to').keyup(function () {
    $(this).val('');
    alert('Please select date from Calendar');
});
$('#from').keyup(function () {
    $('#from,#to').val('');
    $('#to').attr('disabled', 'disabled');
    alert('Please select date from Calendar');
});

});