简体   繁体   English

禁用过去的日期,并仅在日期选择器中启用某些将来的日期

[英]Disable past dates and enable only certain future dates in datepicker

I am attempting to use the jQuery datepicker, but wish to disable all past days and allow only future Thursdays. 我正在尝试使用jQuery datepicker,但希望禁用所有过去的日子,并且仅允许将来的星期四。 I can get it to allow Thursdays, but the minDate functionality I add doesn't work with it. 我可以让它允许星期四,但是我添加的minDate功能不起作用。 This is my code: 这是我的代码:

<script>

jQuery(document).ready(function() {
    jQuery('#input_40_4').datepicker({
        beforeShowDay: function(date) { return [date.getDay() == 4, ""];}
   });
});

$(function () {
    $("#input_40_4'").datepicker({ minDate: 0 });
});

</script>

Can someone explain why these don't work together? 有人可以解释为什么它们不能一起使用吗?

You can add more options to the datepicker by separating them by commas. 您可以通过用逗号分隔datepicker来添加更多选项。

$(document).ready(function() {
    $('#input_40_4').datepicker({
        beforeShowDay: function(date) { return [date.getDay() == 4, ""];},
        minDate : 0
        // you can add more options here as well, just seperate them by commas
   });
});

The reason why they didn't work in your question was because you would overwrite the options from the datepicker created in the function on line 10 with the options from the datepicker in the documentReady section. 它们在您的问题中不起作用的原因是,您将用documentReady部分中datepicker中的选项覆盖在第10行的函数中创建的datepicker中的选项。

You were basically saying create a new datepicker with mindate:0, never mind create a new one with only Tuesdays. 您基本上是在说,用mindate:0创建一个新的日期选择器,没关系,只在星期二创建一个新的日期选择器。

What is happening is you are overwriting the effects of the first datepicker call with the second one. 发生的情况是您用第二个日期选择器调用覆盖了第一个日期选择器调用的效果。

Put both of the options in one options object 将两个选项都放在一个选项对象中

$("#input_40_4'").datepicker({
    beforeShowDay: function(date) { return [date.getDay() == 4, ""];},
    minDate: 0
});

Demo 演示版

 $("#datepicker").datepicker({ beforeShowDay: function(date) { return [date.getDay() == 4, ""];}, minDate: 0 }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script> <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css"></link> <input type="text" id="datepicker" /> 

 $("#datepicker").datepicker({ beforeShowDay: function(date) { return [date.getDay() == 3, ""];}, minDate: 0 }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script> <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css"></link> <input type="text" id="datepicker" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM