我正在尝试编写一个网页,告诉用户输入预订日期,此输入字段包含jQuery日历。我还成功禁用了特定日期,因此没有2位用户预订同一日期。
我的问题是如何从服务器读取文件,获取日期,在jQuery日历中处理和禁用它们?或者有没有办法将数据从PHP发送到JavaScript? JSFiddle。这是我的代码:
var eventDates = ["28/1/2018", "27/1/2018", "2/2/2018"];
$(function() {
$("#iDate").datepicker({
beforeShowDay : function(date) {
var disDate = date.getDate();
var disMonth = date.getMonth();
var disYear = date.getFullYear();
var formattedDate = disDate + "/" + (disMonth + 1) + "/" + disYear;
if ($.inArray(formattedDate, eventDates) != -1) {
return[false]
} else {
return[true]
}
}
});
});
<html>
<head>
<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>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
</head>
<body>
Date: <input id="iDate">
</body>
</html>
答案 0 :(得分:0)
发出如下的ajax请求,php服务将返回禁用日期。
$.get('your_file.php').done(function(eventDates){
$("#iDate").datepicker({
beforeShowDay : function(date) {
var disDate = date.getDate();
var disMonth = date.getMonth();
var disYear = date.getFullYear();
var formattedDate = disDate + "/" + (disMonth + 1) + "/" + disYear;
if ($.inArray(formattedDate, eventDates) != -1) {
return[false]
} else {
return[true]
}
}
});
});
或直接将数组打印到html输出中,如下所示:
<?php
$disabledDates = array("2012-01-01", "2012-02-02");
?>
<script type="text/javascript">
var eventDates = <?php echo json_encode($disabledDates); ?>
$("#iDate").datepicker({
beforeShowDay : function(date) {
var disDate = date.getDate();
var disMonth = date.getMonth();
var disYear = date.getFullYear();
var formattedDate = disDate + "/" + (disMonth + 1) + "/" + disYear;
if ($.inArray(formattedDate, eventDates) != -1) {
return[false]
} else {
return[true]
}
}
});
</script>