在今天的datepicker后2天禁用

时间:2016-12-29 04:46:31

标签: javascript php jquery datepicker

我想在今天之后使用jquery datepicker禁用2天,例如今天是2016年12月29日我希望启用日期为1/1/2017。我在jsfiddle中查看某人的代码,但我不知道应该编辑什么 https://jsfiddle.net/AcbUf/574/

HTML

<input type="text" id="from" />
<input type="text" id="to" />

的js

$(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: "+3d",
    minDate: 1,
    maxDate: "+3M",
    dateFormat: 'dd M yy',
    showOtherMonths: true,
    changeMonth: true,
    selectOtherMonths: true,
    required: true,
    showOn: "focus",
    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+'d';
    date_diff = date_diff + 'd';
    $('#to').val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
        dateFormat: 'dd.mm.yy',
        minDate: date_diff,
        maxDate: maxDate_d
    });
});

$('#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');
});
});

3 个答案:

答案 0 :(得分:3)

您可以将(SELECT * FROM users WHERE age = 16 ORDER BY random() LIMIT 20) UNION ALL (SELECT * FROM users WHERE age = 17 ORDER BY random() LIMIT 20) UNION ALL ... 变量设置为&#39; 3&#39;。所以它将从你当前日期的两天开始:

检查一下:

设置minDate

答案 1 :(得分:1)

根据您的问题,请尝试使用此新代码

此处还有小提琴链接https://jsfiddle.net/avadh/21829x0b/

$(document).ready(function () {
var dt = new Date();
dt.setDate(dt.getDate() + 2);
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: "+3d",
    minDate: dt,
    maxDate: "+3M",
    dateFormat: 'dd M yy',
    showOtherMonths: true,
    changeMonth: true,
    selectOtherMonths: true,
    required: true,
    showOn: "focus",
    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+'d';
    date_diff = date_diff + 'd';
    $('#to').val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
        dateFormat: 'dd.mm.yy',
        minDate: date_diff,
        maxDate: maxDate_d
    });
});

$('#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');
});

});

答案 2 :(得分:0)

您需要提供日期加两天,如:

var app = angular.module('form-example1', []);

var INTEGER_REGEXP = /^-?\d+$/;
app.directive('integer', function() {
  return {
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
      ctrl.$validators.integer = function(modelValue, viewValue) {
        if (ctrl.$isEmpty(modelValue)) {
          // consider empty models to be valid
          return true;
        }

        if (INTEGER_REGEXP.test(viewValue)) {
          // it is valid
          return true;
        }

        // it is invalid
        return false;
      };
    }
  };
});