使用星期二和星期二设置datepicker中的默认日期周三只? [英] set Default date in datepicker with Tuesdays & Wednesdays only?

查看:215
本文介绍了使用星期二和星期二设置datepicker中的默认日期周三只?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期选择器,其所有过去几天都被禁用,除星期二和星期三之外的所有日子都被禁用。

I have a datepicker that has all past days disabled, and all days except Tuesdays and Wednesdays disabled.

我希望默认日期是一周中启用的第一天。如果它是星期二,那么它应该显示星期二,如果星期三,然后星期三。任何其他日子它应该在输入字段的下周二显示。

I'd like the default day to be the frist enabled day of the week. If it's tuesday, then it should display tuesday, If wednesday, then wednesday. Any other day it should display next tuesday in the input field.

这是我用于日期选择器的代码。

This is the code I'm using for the datepicker.

<script type="text/javascript">   
    $(document).ready(function(){
        $( ".datepicker" ).datepicker({
            minDate:'0',
            changeMonth:true,
            changeYear:true,
        beforeShowDay:
              function(dt)
              {
                  return [dt.getDay() === 2 || dt.getDay() === 3, ""];
              }});
        });
</script>

我正在使用选择器的输入:

And the input that I'm using the selector on:

<form method="POST">
    <div style='margin:0 auto; text-align:center;'>
        <label>Appointment Date: </label>
        <input type="hidden" name="action" value="list_appointments" />
        <input type="text" id="datepicker" name="date" class="datepicker" />
               <!--value=--><?php #echo date_format(new DateTime("now",new DateTimeZone('America/Chicago')),'m/d/Y');?> 
        <input type="submit" value="Refresh List" name="Update" />
        <br />
    </div>
</form>


推荐答案

你可以使用 momentjs 让一周的第二天变得更容易,然后只需要一个if-else语句来获得正确的一周:

You can use momentjs to make getting the next day of the week a little easier and then just have a if-else statement to get the correct day of the week:

 function setDate() {
   var today = moment().isoWeekday(); //or just = new Date().getDay();
   if (today <= 2) {
     return moment().isoWeekday(2);
   } else if (today === 3) {
     return moment().isoWeekday(3);
   } else {
     return moment().add(1, 'weeks').isoWeekday(2);
   }
 }

调用 setDate() 函数然后将该返回值设置为datepicker中的日期。

Call the setDate() function then and set that return value as the date in the datepicker.

 $('.datepicker').datepicker("setDate", new Date(setDate()));

这是一个有效的例子: https://jsfiddle.net/jutcw0ve/

Here's a working example: https://jsfiddle.net/jutcw0ve/

这篇关于使用星期二和星期二设置datepicker中的默认日期周三只?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆