Bootstrap Datepicker Disable Past Dates After Firs

2020-04-11 11:37发布

I'm working on a Wordpress website with the theme based on Bootstrap 3.

In a form I need to include calendar date pickers. I added a js library from eyecon (Demo Website for Datepicker Library). I made it work with date selectors, but I'm having trouble disabling choosing the past times for arrival field, when the departure time is set.

For example, if the Departure time is set to be 1 March, when the user clicks on the Arrival field in the calendar that pops up, all the dates before 2nd March should be disabled.

I tried multiple versions of code given on demo website, but they didn't work. I think the problem might be that I have jQuery in noConflict mode.

My current script is located in footer.php of my theme. The calendar pop up works, and it disables past dates, but it doesn't disable dates before the Arrival field once the Departure date is set.

This was the most successful attempt, as at least the calendar popups were working on this attempt: http://jsfiddle.net/j5ZKc/

And this is my current script:

<script type="text/javascript">
jQuery(function() {
  jQuery('#dp1, #dp2'
  ).datepicker({startDate: '-0m'}).on('changeDate', function(){
    jQuery( '#dp1, #dp2'
    ).datepicker('hide');
  })
});
</script>

Any suggestions please?

2条回答
闹够了就滚
2楼-- · 2020-04-11 11:56

So after some trouble I finally got something working for you:

jsFiddle Bootstrap-datepicker

What did I do:

I used forked Bootstrap-datepicker. It is in my opinion way better documented and easier to use. I got it working so that should count for something. For example I got the date of the respective datepicker by invoking getDate() on a datepicker:

checkout.datepicker("getDate").valueOf()

If you have any questions about my code, let me know!

EDIT:

I updated the former jsFiddle (see the link above). It automatically sets the value of #dp2 to the selected date of #dp1 + one day. Therefore you should always be in the correct month.

If you don't want the code to put the focus on #dp2 after a date is selected just remove the following line in the jsFiddle:

$('#dp2')[0].focus();
查看更多
Fickle 薄情
3楼-- · 2020-04-11 11:56

Try this code snippet:

$('#changestartdate').datetimepicker({ minDate:new Date() });

查看更多
登录 后发表回答