简体   繁体   English

在jquery ui datepicker中,如果选择了星期六,如何更改timepicker.co的时间?

[英]In jquery ui datepicker how can I can I make timepicker.co change time if Saturday is chosen?

At the moment I am struggling in making Datepicker change timepicker.co options if Saturday is chosen. 目前,如果选择周六,我将努力使Datepicker更改timepicker.co选项。

$(".selector").datepicker({  
if(Saturday chosen) {  
Time changed 7am - 2pm  
      }
   })

Any ideas how to do it? 有什么想法怎么做?

Thank you! 谢谢!

Had some time, and made a demo for you. 有一段时间,并为您制作了一个演示。

First, look at the available demos: http://jsfiddle.net/wvega/A9cE6/embedded/result,js,html,css,resources/ 首先,查看可用的演示: http : //jsfiddle.net/wvega/A9cE6/embedded/result,js,html,css,resources/

You will first need some association between the weekday selected, and the hours that should be set. 首先,您需要在所选的工作日和应设置的小时之间建立某种关联。 I did this using an object of arrays. 我使用数组对象完成此操作。

var hours = {
  sunday: [null, null],
  monday: [6, 22],
  tuesday: [6, 22],
  wednesday: [6, 22],
  thursday: [6, 22],
  friday: [6, 22],
  saturday: [7, 14],
};

Made some guesses there. 在那里做了一些猜测。 Could do this when the selection is made, but felt this was a bit easier. 做出选择时可以执行此操作,但是感觉有点容易。

Made some other guesses since you had not included an example or a complete amount of code. 由于您还没有提供示例或完整的代码,因此做出了其他一些猜测。

HTML 的HTML

<div class="ui-widget" style="width: 340px;">
  <div class="ui-widget-header ui-corner-top">Date &amp; Time</div>
  <div class="ui-widget-content ui-corner-bottom">
    <ul>
      <li>
        <label>Select Date</label>
        <input type="text" class="date selector" id="date-1" />
      </li>
      <li>
        <label>Select Time</label>
        <input type="text" class="time selector" id="time-1" /> -
        <input type="text" class="time selector" id="time-2" />
      </li>
    </ul>
    <button id="date-time-save">Set Date &amp; Time</button>
  </div>
</div>

Here we have a date and two time fields. 在这里,我们有一个日期和两个时间字段。 This is a basic wrapper just to look nice. 这是一个基本的包装,只是看起来不错。

JavaScript 的JavaScript

var hours = {
  sunday: [null, null],
  monday: [6, 22],
  tuesday: [6, 22],
  wednesday: [6, 22],
  thursday: [6, 22],
  friday: [6, 22],
  saturday: [7, 14],
};

$(function() {
  $(".date").datepicker({
    dateFormat: "DD m/d/y",
    onSelect: function(dt, $dtp) {
      var selectWeekDay = dt.slice(0, dt.indexOf(" ")).toLowerCase();
      var selectHours = hours[selectWeekDay];
      if (selectWeekDay === "sunday") {
        $(".time").val("");
        return true;
      }
      var hour1 = selectHours[0] < 12 ? selectHours[0] + ":00a" : selectHours[0] + "00p";
      var hour2 = selectHours[1] < 12 ? selectHours[1] + ":00a" : selectHours[1] + ":00p"
      $("#time-1").timepicker("setTime", hour1);
      $("#time-2").timepicker("setTime", hour2);
    }
  });
  $(".time").timepicker({
    timeFormat: 'h:mm p',
    interval: 30,
    minTime: '6',
    maxTime: '10:00pm',
    startTime: '6:00',
    dynamic: false,
    dropdown: true,
    scrollbar: true
  });

  $("#date-time-save").button({
    icon: "ui-icon-disk",
    click: function(e) {
      e.preventDefault();
      return false;
    }
  });
});

Working Example: https://jsfiddle.net/Twisty/r1dfLr9r/ 工作示例: https : //jsfiddle.net/Twisty/r1dfLr9r/

You can see when the user selects a date, date text is passed back to the onSelect callback. 您可以看到用户选择日期时,日期文本将传递回onSelect回调。 There are some more difficult ways to get this data if you want, yet I wanted it to be easy so I just sliced off the text I needed. 如果需要,还有一些更困难的方法来获取这些数据,但是我希望它很容易,所以我只切了需要的文本。

We cross reference that as the Index in our hours object and now we know what hours we want to set. 我们将其作为hours对象中的索引进行交叉引用,现在我们知道要设置什么小时了。 We can then make use of the setTime method of timepicker. 然后,我们可以使用时间选择器的setTime方法。

Timepicker will take a few formats for setTime , the easiest is a string, like "7:00a" . timepicker将为setTime采用几种格式,最简单的是字符串,例如"7:00a" So we add a little format code as we pull out the hours we want for that day. 因此,我们在拉出当天所需时间的同时添加了一些格式代码。 We then set the time values. 然后,我们设置时间值。 You can also enter these strings in the array if you want. 如果需要,还可以在数组中输入这些字符串。 Something like: 就像是:

var hours = {
  sunday: [null, null],
  monday: ["6:00a", "11:00p"],
  tuesday: ["6:00a", "11:00p"],
  wednesday: ["6:00a", "11:00p"],
  thursday: ["6:00a", "11:00p"],
  friday: ["6:00a", "11:00p"],
  saturday: ["7:00a", "2:00p"],
};

Your preference. 您的偏好。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改Jquery UI Datepicker中的日期格式 - How can i change date format in Jquery UI datepicker 我无法让Jquery UI timepicker插件正常工作 - I can't get Jquery UI timepicker plugin to work 我如何在 datepicker jquery UI 上保存日期的值 - How can i save the value of the date on datepicker jquery UI 如何从Ajax响应重新填充jquery UI datepicker - How can I repopulate the jquery UI datepicker from an Ajax response 我如何使用JQuery倒数开始对Bootstrap Datepicker的“ change”事件之后的剩余时间进行计数? - How can I use JQuery countdown to start counting the remaining time after the “change” event from a bootstrap Datepicker? 我如何禁用角度ui Timepicker中的分钟输入 - How i can disable minutes input in angular ui Timepicker 如何点击时间戳时获取当前时间? - How can I get the current time when the timepicker is clicked? 用户选择时间后如何使用类星体时间选择器 - How can I the quasar timepicker after the user has picked a time 如何让 Timepicker 出现在 Angular 的下拉列表中? - How can I make a Timepicker appear in Dropdown in Angular? 我可以使用 Jquery UI 日期选择器来更改键盘输入日期的格式吗? - Can I use Jquery UI datepicker to change format of a keyboard-entered date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM