jQuery 日期选择器特定日期禁用不起作用
jQuery datepicker specific date disable is not working
我有代码可以禁用 jquery 日期选择器上的特定日期。代码 运行 在我的本地环境中很好,但在 GoDaddy windows 托管
上不起作用
var disableddates = result; //result is array of dates ["1/7/2018","1/8/2018","1/9/2018"]
$("#txtFromdate").datepicker({
minDate: 0,
beforeShowDay : DisableSpecificDates
});
$("#txtTodate").datepicker({
minDate: 0,
beforeShowDay : DisableSpecificDates
});
function DisableSpecificDates(date) {
var string = jQuery.datepicker.formatDate('mm/dd/yy', date);
console.log(string); //this string return with leading zero 01/07/2018...
return [disableddates.indexOf(string) == -1];
}
此代码 运行 在本地运行良好,但在启用时不会禁用 jQuery 日期选择器中的特定日期。
如果日期已经是日期格式,则不需要格式化日期。例如,如果 result
包含类似 "1/7/2018"
的数组日期,您可以从中创建一个 Date 对象:
var newDate = new Date(result[0]);
考虑到这一点,您可以轻松地创建和操作日期。 DatePicker 也将 Date 对象传递给函数。您可以尝试以下解决方案:
$(function() {
var disableddates = ["1/7/2018", "1/8/2018", "1/9/2018"];
$("#txtFromdate, #txtTodate").datepicker({
minDate: 0,
beforeShowDay: DisableSpecificDates,
dateFormat: "m/d/yy"
});
$("#txtFromdate").change(function() {
$("#txtTodate").datepicker("option", "minDate", $(this).val());
});
function DisableSpecificDates(date) {
var result = [true];
$.each(disableddates, function(k, v) {
var exclude = new Date(v).toString();
var today = date.toString();
if (exclude == today) {
console.log("Match", exclude, today);
result = [false];
}
});
return result;
}
});
label {
width: 60px;
}
input {
width: 10em;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<label for="txtFromdate">From:</label>
<input type="text" id="txtFromdate" />
<label for="txtTodate">To:</label>
<input type="text" id="txtTodate" />
我在这里完成的测试:https://jsfiddle.net/95t81vzq/3/
希望对您有所帮助。
我有代码可以禁用 jquery 日期选择器上的特定日期。代码 运行 在我的本地环境中很好,但在 GoDaddy windows 托管
上不起作用var disableddates = result; //result is array of dates ["1/7/2018","1/8/2018","1/9/2018"]
$("#txtFromdate").datepicker({
minDate: 0,
beforeShowDay : DisableSpecificDates
});
$("#txtTodate").datepicker({
minDate: 0,
beforeShowDay : DisableSpecificDates
});
function DisableSpecificDates(date) {
var string = jQuery.datepicker.formatDate('mm/dd/yy', date);
console.log(string); //this string return with leading zero 01/07/2018...
return [disableddates.indexOf(string) == -1];
}
此代码 运行 在本地运行良好,但在启用时不会禁用 jQuery 日期选择器中的特定日期。
如果日期已经是日期格式,则不需要格式化日期。例如,如果 result
包含类似 "1/7/2018"
的数组日期,您可以从中创建一个 Date 对象:
var newDate = new Date(result[0]);
考虑到这一点,您可以轻松地创建和操作日期。 DatePicker 也将 Date 对象传递给函数。您可以尝试以下解决方案:
$(function() {
var disableddates = ["1/7/2018", "1/8/2018", "1/9/2018"];
$("#txtFromdate, #txtTodate").datepicker({
minDate: 0,
beforeShowDay: DisableSpecificDates,
dateFormat: "m/d/yy"
});
$("#txtFromdate").change(function() {
$("#txtTodate").datepicker("option", "minDate", $(this).val());
});
function DisableSpecificDates(date) {
var result = [true];
$.each(disableddates, function(k, v) {
var exclude = new Date(v).toString();
var today = date.toString();
if (exclude == today) {
console.log("Match", exclude, today);
result = [false];
}
});
return result;
}
});
label {
width: 60px;
}
input {
width: 10em;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<label for="txtFromdate">From:</label>
<input type="text" id="txtFromdate" />
<label for="txtTodate">To:</label>
<input type="text" id="txtTodate" />
我在这里完成的测试:https://jsfiddle.net/95t81vzq/3/
希望对您有所帮助。