简体   繁体   English

如何动态设置jQuery Mobile的日期框的minHour选项

[英]How to dynamically set minHour option of jquery mobile's datebox

I have two inputs of type="text" date-role="datebox" 我有两个类型为“文本”的输入date-role =“ datebox”

One is the start time. 一是开始时间。 The other is the end time. 另一个是结束时间。

What I would like to do is that when the user picks the start time, I change the minHour option of the end time datebox to the hour they selected. 我想做的是,当用户选择开始时间时,我将结束时间日期框的minHour选项更改为他们选择的小时。

Is that possible? 那可能吗? If so, how? 如果是这样,怎么办?

Thanks 谢谢

From what I can see minHour seems to default the time picker popup to the right hour, but it then lets you pick an earlier hour anyway. 从我可以看到,minHour似乎默认将时间选择器弹出窗口设置为正确的时间,但是无论如何它可以让您选择更早的时间。 If you pick an earlier hour than minHour, the datebox displays the earlier hour, but when you call datebox('getTheDate'); 如果您选择的时间比minHour更早,则日期框会显示更早的时间,但在您调用datebox('getTheDate')时会显示; it returns the minHour. 它返回minHour。 So I came up with a workaround that might do the job for you. 因此,我想出了一种解决方法,可以为您完成这项工作。

Here is a DEMO 这是一个演示

Each of the dateboxes has callback for onclose: 每个日期框都有onclose的回调:

<input name="Date1" id="Date1" type="date" data-role="datebox" 
data-options='{"mode": "timebox", "closeCallback": "Date1Close();"}' /> 
...
<input name="Date2" id="Date2" type="date" data-role="datebox" 
data-options='{"mode": "timebox", "closeCallback": "Date2Close();"}' /> 

The callback on the starttime gets the hour of the selected time and then sets the minHour of the endtime box to that hour + 1. Then the 'workaround' is the callback for the endtime which sets the time of the box to the 'getTheDate' of the box in order to correct the display. 开始时间的回调获取选定时间的小时,然后将结束时间框的minHour设置为该小时+1。然后,“解决方法”是结束时间的回调,它将结束时间框的时间设置为“ getTheDate”为了更正显示框。

function Date1Close(){
    var startdate = $('#Date1').datebox('getTheDate');
    var minH = startdate.getHours() + 1;
    if (minH > 23) minH = 23;
    $('#Date2').datebox({"minHour": minH});
}

function Date2Close(){
    var enddate = $('#Date2').datebox('getTheDate');
    $('#Date2').datebox('setTheDate', enddate);
    $('#Date2').trigger('datebox', {'method':'doset'});
}

In the second callback you could also just compare the endtime to the startime and if end is less than start, set the second datebox to the starttime. 在第二个回调中,您也可以仅将结束时间与星空时间进行比较,如果end小于开始时间,则将第二个日期框设置为开始时间。

You might want to contact the Datebox developer to see if there is a bug with the minHour feature... 您可能需要联系Datebox开发人员,以查看minHour功能是否存在错误...

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

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