当具有multidatepicker时,dateSeicker OnSelect不起作用

时间:2016-03-02 09:20:26

标签: javascript jquery jquery-ui datepicker jquery-multidatespicker

我使用jquery-ui datepicker创建了一个内联日历,我也使用了multiDatePicker。但是点击某个特定的按钮,我希望删除分配到multiDatePicker的所有日期,只显示所选的点击日期。

我的代码是这样的:

<!doctype html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <title>jQuery UI Datepicker functionality</title>
      <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <link rel="stylesheet prefetch" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/smoothness/jquery-ui.css">

  <script src="http://code.jquery.com/jquery-2.1.4.js"></script>
  <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <script type="text/javascript" src="js/jquery-ui.multidatespicker.js"></script>


  <script>
     $(function() {

        var today = new Date();
        var yesterday = (new Date()).setDate(today.getDate()-1); 
        var tomorrow = (new Date()).setDate(today.getDate()+1);
        var dayAfttomorrow = (new Date()).setDate(today.getDate()+2);
        var dayDayAfttomorrow = (new Date()).setDate(today.getDate()+3);
        var dates = [yesterday, today, tomorrow, dayAfttomorrow, dayDayAfttomorrow];


        $('#calender').multiDatesPicker({
           disabled: false,
           addDates: dates
        });

        $( "#calender" ).datepicker({ 
            changeYear: true,
            changeMonth: true,
            onSelect: function(dateStr) {
              // $('#calender').multiDatesPicker('resetDates', true);
              var date = $(this).datepicker('getDate');
              alert(date);
            }
        });

     });

  </script>
  <style type="text/css">
  /*.ui-state-highlight,*/ .ui-widget-content .ui-state-highlight, .ui-widget-header .ui-state-highlight {
     border: 1px solid green !important;
     /*background: green;*/
  }
  .ui-state-active, .ui-widget-content .ui-state-active, .ui-widget-header .ui-state-active {
     border: 1px solid #d3d3d3 !important;
  }
  .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {
     border: none !important;
  }
  /*.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {
     background: green;
  }*/
  </style>


</head>
   <body>
      <!-- HTML --> 
      <div id="calender"></div>
   </body>
</html>

在这里,我无法在onSelect中获得警报?任何人都可以帮助我......

1 个答案:

答案 0 :(得分:2)

您可以将onSelect传递给multiDatesPicker

$(function() {

  var today = new Date();
  var yesterday = (new Date()).setDate(today.getDate() - 1);
  var tomorrow = (new Date()).setDate(today.getDate() + 1);
  var dayAfttomorrow = (new Date()).setDate(today.getDate() + 2);
  var dayDayAfttomorrow = (new Date()).setDate(today.getDate() + 3);
  var dates = [yesterday, today, tomorrow, dayAfttomorrow, dayDayAfttomorrow];


  $('#calender').multiDatesPicker({
    addDates: dates,
    onSelect: function(dateStr) {
      // $('#calender').multiDatesPicker('resetDates', true);
      var date = $(this).datepicker('getDate');
      $('#calender').multiDatesPicker('resetDates').multiDatesPicker('addDates', [date]);
      console.log($('#calender').multiDatesPicker('getDates'))
    }
  });
});
/*.ui-state-highlight,*/

.ui-widget-content .ui-state-highlight,
.ui-widget-header .ui-state-highlight {
  border: 1px solid green !important;
  /*background: green;*/
}
.ui-state-active,
.ui-widget-content .ui-state-active,
.ui-widget-header .ui-state-active {
  border: 1px solid #d3d3d3 !important;
}
.ui-state-default,
.ui-widget-content .ui-state-default,
.ui-widget-header .ui-state-default {
  border: none !important;
}
/*.ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {
     background: green;
  }*/
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<link rel="stylesheet prefetch" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/smoothness/jquery-ui.css">

<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="http://multidatespickr.sourceforge.net/jquery-ui.multidatespicker.js"></script>



<div id="calender"></div>