我正在使用此datepicker和此timepicker。我想開發JS代碼,它將確定用戶是否選擇了datepicker上的當前日期,如果是這樣,將觸發timepicker的函數,該函數將禁用實際時間之前的所有時間。我是JavaScript新手,所以我只得到了很多。基於另一個字段自動更改字段javascript
HTML:
<input id="datemax" placeholder="Enter the date of incident" type="text"/>
<input id="timemax" placeholder="Enter the time of incident" type="text"/>
功能到當前時間之前禁用時間,這工作,但將保持不變,無論選擇何種日期:
function getCurrentTime(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0'+minutes : minutes;
return hours + ':' + minutes + ' ' + ampm;
}
var timesAvailable = {
'timeFormat': 'h:i A',
'disableTimeRanges': [[getCurrentTime(new Date()), '12am']]
};
$('#timemax').timepicker(timesAvailable);
我想知道什麼會被要求實施把這個JS函數放到我的表單中。我認爲需要開發第二個函數,並且需要使用var datemax = document.getElementById(「datemax」).value;從日期字段中調用該值。
我已經開發了一點不完整的僞代碼在這裏:
function datetimeSelect{
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth();
var year = currentDate.getFullYear();
var datemax = document.getElementById("datemax").value;
if (datemax === currentDate){
//activate
'disableTimeRanges': [[getCurrentTime(new Date()), '12am']] //Can't select any time before now, greyed out
return false;
}
else //datemax does not equal to currentDate
{
$('#timeMax').timepicker();//Standard Datepicker and any time can be selected
return true;
{
因此,例如,如果選擇了今天這不是一個日期,timepicker將返回到它的默認功能沒有任何灰色倍。
還有我前面指出,你可以掛接到'日期選擇我的意思是change'事件如果當用戶選擇某個日期,你可以運行一個函數,如果是的話,我可能有一個解決方案。我從來沒有使用過這個插件,所以當我去看看他們的文檔,我找不到任何事件 – Viney
是的,這可能是一個相關的解決方案。請張貼它,讓我可以看看。 – MrMaestro