简体   繁体   中英

bootstrap-datepicker: Show DIV when selecting specific date

Using the bootstrap-datepicker plugin with the embedded markup...

How to show/reveal a DIV element when selecting a specific date AND hide it again when another date has been selected?

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>

  <style>
    #example {
      display: none;
      color: green;
      font-weight: bold;
    }

  </style>
</head>

<body>
  <div id="example">Yay my favorate day!</div>
  Please select <b>11/07/2017</b> to show an example:
  <br />
  <div id="myCalendar"></div>
  <input type="hidden" id="hiddenInput">

  <script>
    $(function() {
      $('#myCalendar').datepicker({
        format: 'mm-dd-yyyy',
        weekStart: 1,
        maxViewMode: 2,
        todayBtn: "linked",
        daysOfWeekHighlighted: "0,6",
        todayHighlight: true
      }).on('changeDate', function() {
        $('#hiddenInput').val(
          $('#myCalendar').datepicker('getFormattedDate')
        );
      }).change(function() {
        $("#example").toggle($(this).val() === "11-07-2017");
      });
    });

  </script>
</body>

Demo on JSFiddle

You can do this by checking the value of the datepicker . For this example I use 11/07/2017 as the date you are checking for (aka November 17th, 2017).

 $(function(){ $('.datepicker').datepicker({ format: 'mm-dd-yyyy' }).change(function () { $("#example").toggle($(this).val() === "11-07-2017"); }); }); 
 #example { display:none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script> <div id="example">Yay my favorate day!</div> Please select <b>11/07/2017</b> to show an example: <br /> <input type="text" class="datepicker" placeholder="Date..." name="date"> 

There are several ways for different frameworks (and vanilla js). For example, an easy one, if you are using angularjs (not much javascript code is required):

<input data-provide="datepicker" ng-model='yourdate'>
<div id='divExample' ng-show='yourdate==somevalue'></div>

You can use changeDate event, as the docs says:

All events have extra data attached to the event object that is passed to any event handlers

  • date : the relevant Date object, in local timezone. For a multidate picker, this will be the latest date picked.

You can check what is the selected day (using setter like getDate() , getMonth() and getFullYear() ) and the using jQuery show() and hide() .

Here a live sample:

 $(function() { $('#myCalendar').datepicker({ format: 'mm-dd-yyyy', weekStart: 1, maxViewMode: 2, todayBtn: "linked", daysOfWeekHighlighted: "0,6", todayHighlight: true }).on('changeDate', function(e) { if( e.date && e.date.getDate() == 7 && e.date.getMonth() == 10 && e.date.getFullYear() == 2017 ){ $("#example").show(); } else { $("#example").hide(); } $('#hiddenInput').val( $('#myCalendar').datepicker('getFormattedDate') ); }); }); 
 #example { display: none; color: green; font-weight: bold; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script> <div id="example">Yay my favorate day!</div> Please select <b>11/07/2017</b> to show an example: <br /> <div id="myCalendar"></div> <input type="hidden" id="hiddenInput"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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