如何在html日期表单中使某些日期不可用

问题描述 投票:2回答:2

我有一张预订表格,我有一份日期表格。我想禁用已预订/不可用的特定日期。我有JavaScript代码,在我的情况下不起作用。有没有其他方法可以帮助我禁用日历中的日期?

var array = ["2019-03-14", "2019-03-11", "2019-03-26"];

$(function () {
  $('input').datepicker({
    dateFormat: 'yy-mm-dd',
    beforeShowDay: function(date) {
      var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      return [array.indexOf(string) == -1]
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />

<div class="form-group">
  <label for="date">Book your date</label>
  <input type="date" name="date" class="form-control" placeholder="" required readonly>
</div> 
     
jquery html datetime jquery-ui-datepicker html-form
2个回答
1
投票

您的代码似乎运行良好。我唯一改变的是:

  • 添加了dateFormatyy-mm-dd选项,因为这是<input type="date">所需的格式,
  • 将输入标记为readonly,因此它只能通过popin进行修改(请注意,这可能不是理想的可访问性)。

var array = ["2019-03-14", "2019-03-11", "2019-03-26"];

$(function () {
  $('input').datepicker({
    dateFormat: 'yy-mm-dd',
    beforeShowDay: function(date) {
      var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      return [array.indexOf(string) == -1]
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />

<div class="form-group">
  <label for="date">Book your date</label>
  <input type="date" name="date" class="form-control" placeholder="" required readonly>
</div>

0
投票

请在<head> </head>之间的任何地方添加:

  <!-- datepicker -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.min.js" type="text/javascript"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="Stylesheet" type="text/css">
  <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script type="text/javascript" src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <!-- //datepicker -->
© www.soinside.com 2019 - 2024. All rights reserved.