javascript - Jquery-如何设置今天之前的日期无法选择?
问题描述
我可以让今天的日期出现在入住日期,但问题是用户仍然可以选择今天之前的日期。有什么解决方案可以让我解决他的问题吗?任何建议都会有很大帮助,谢谢!
$(function () {
$("#chkI").datepicker({
dateFormat: "yy-mm-dd", showAnim: "slideDown",
onClose: function (selectedDate) {
var minDate = $(this).datepicker('getDate');
var newMin = new Date(minDate.setDate(minDate.getDate() + 1));
$("#chkO").datepicker("option", "minDate", newMin);
}
});
var currentDate = new Date();
$("#chkI").datepicker("setDate", currentDate);
$("#chkO").datepicker({ dateFormat: "yy-mm-dd", showAnim: "slideDown",
onClose: function (selectedDate) {
var maxDate = $(this).datepicker('getDate');
var newMax = new Date(maxDate.setDate(maxDate.getDate() - 1));
$("#chkI").datepicker("option", "maxDate", newMax);
}
});
});
解决方案
您只需要设置 minDate 并且不要为相同的输入再次初始化 datepicker
$(function () {
$("#chkI").datepicker({
minDate: new Date(), // start date should be today's date
dateFormat: "yy-mm-dd", showAnim: "slideDown",
onClose: function (selectedDate) {
var minDate = $(this).datepicker('getDate');
var newMin = new Date(minDate.setDate(minDate.getDate() + 1));
$("#chkO").datepicker("option", "minDate", newMin);
}
}).datepicker("setDate", new Date()); // select today's date by default
//var currentDate = new Date();
//$("#chkI").datepicker("setDate", currentDate);
$("#chkO").datepicker({ dateFormat: "yy-mm-dd", showAnim: "slideDown",
onClose: function (selectedDate) {
var maxDate = $(this).datepicker('getDate');
var newMax = new Date(maxDate.setDate(maxDate.getDate() - 1));
$("#chkI").datepicker("option", "maxDate", newMax);
}
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="chkI">
<input type="text" id="chk0">
推荐阅读
- python - Python Pandas Dataframe-将索引设置为具有自定义月份的日期时间
- ruby-on-rails - rails 5.0.7 中“image”的未定义方法 `[]':Sass::Script::Value::String
- asp.net-core - HTTP 错误 502.5 - ASP.NET Core 2.1 应用程序中的进程失败
- c++ - 按值从向量中查找和替换元素
- java - 从 Java 运行 plink 版本的 PuTTY
- python - 使用 2 级嵌套数组将数据帧转换为 JSON
- android-recyclerview - RecyclerView 里面 RecyclerView 里面 RecyclerView
- typescript - 使用布尔常量检查参数是否定义
- javascript - Materialize.css: Expand the first Collapsible div on Page load
- java - 取消无限循环