我有一个WordPress网站并创建了一个自定义页面(由于连接了一个单独的数据库)。在这个自定义页面上,我正在创建一个高级搜索表单。在搜索表单上,我有一个开始日期datepicker和一个结束日期datepicker。
出于某些明显的原因,我似乎无法将jQuery日期选择器甚至HTML日期选择器用于我的自定义表单编码。我找到了一个有点工作的JavaScript,但问题是当你选择一个日期时,它会把第二天的日期放在文本框中。而另一个问题是日历看起来很糟糕。
现在此表单上还有其他方面,但它们都正常工作。我正在使用PHP和PostgreSQL来获取研究信息。我有下面的标题和表单代码。并提供日期选择器的JavaScript代码链接。
如何更好地选择日期和CSS?
标题:
<link rel="stylesheet" type="text/css" href="/VCSWeb/wp-content/themes/i-excel-child/css/datepicker.css" />
<script type="text/javascript" src="/VCSWeb/wp-content/themes/i-excel-child/js/datepicker.js">
<script type="text/javascript" src="/VCSWeb/wp-content/themes/i-excel-child/js/timepicker.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker("option","dateFormat","yy-mm-dd");
$( "#format" ).change(function() {
$( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
});
});
</script>
形式:
<form role="search" name="remarks_lookup" method="post" class="search-form" action="#samepage" >
<div style="padding-left:10px; width:25%; float:left; margin-top:-20px; ">
<label style="font-size:12px;">Start Date: <br />
<input style="height:22px;" id="start_dt" name="start_date" class='datepicker boxstyle' size='20' placeholder="YYYY/MM/DD" value="" title='YYYY-MM-DD' />
</label>
</div>
<div style="padding-left:10px; width:75%; float:left; margin-top:-20px;">
<label>End Date: <br />
<input style="height:22px;" id="end_dt" name="end_date"class='datepicker boxstyle' placeholder="YYYY/MM/DD" size='20' value="" title='YYYY-MM-DD' />
</label>
</div>
</form>
DatePicker Js:
// Converts a date into '2016-04-29' format
function getDateString(dt) {
return dt.getFullYear() + '-' +
['01','02','03','04','05','06','07','08','09','10','11','12'][dt.getMonth()] +
'-' + ['01','02','03','04','05','06','07','08','09','10','11','12', '13', '14', '15', '16', '17', '18', '19', '20','21','22','23','23','24','25','26','27','28','29','30','31'][dt.getDate()];
}
// Converts a date into 'July 2010' format
function getMonthYearString(dt) {
return ['January','February','March','April','May','June','July',
'August','September','October','November','December'][dt.getMonth()] +
' ' + dt.getFullYear();
}
// This is the function called when the user clicks any button
function chooseDate(e) {
var targ; // Crossbrowser way to find the target (http://www.quirksmode.org/js/events_properties.html)
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
var div = targ.parentNode.parentNode.parentNode.parentNode.parentNode; // Find the div
var idOfTextbox = div.getAttribute('datepickertextbox'); // Get the textbox id which was saved in the div
var textbox = document.getElementById(idOfTextbox); // Find the textbox now
if (targ.value=='<' || targ.value=='>' || targ.value=='<<' || targ.value=='>>') { // Do they want the change the month?
createCalendar(div, new Date(targ.getAttribute('date')));
return;
}
textbox.value = targ.getAttribute('date'); // Set the selected date
div.parentNode.removeChild(div); // Remove the dropdown box now
}
// Parse a date in d-MMM-yyyy format
function parseMyDate(d) {
if (d=="") return new Date('NotADate'); // For Safari
var a = d.split('-');
if (a.length!=2) return new Date(d); // Missing 2 dashes
var m = -1; // Now find the month
if (a[1]=='01') m=0;
if (a[1]=='02') m=1;
if (a[1]=='03') m=2;
if (a[1]=='04') m=3;
if (a[1]=='05') m=4;
if (a[1]=='06') m=5;
if (a[1]=='07') m=6;
if (a[1]=='08') m=7;
if (a[1]=='09') m=8;
if (a[1]=='10') m=9;
if (a[1]=='11') m=10;
if (a[1]=='12') m=11;
if (m<0) return new Date(d); // Couldn't find the month
var d = -1; // Now find the month
if (a[1]=='01') d=0;
if (a[1]=='02') d=1;
if (a[1]=='03') d=2;
if (a[1]=='04') d=3;
if (a[1]=='05') d=4;
if (a[1]=='06') d=5;
if (a[1]=='07') d=6;
if (a[1]=='08') d=7;
if (a[1]=='09') d=8;
if (a[1]=='10') d=9;
if (a[1]=='11') d=10;
if (a[1]=='12') d=11;
if (a[1]=='13') d=12;
if (a[1]=='14') d=13;
if (a[1]=='15') d=14;
if (a[1]=='16') d=15;
if (a[1]=='17') d=16;
if (a[1]=='18') d=17;
if (a[1]=='19') d=18;
if (a[1]=='20') d=19;
if (a[1]=='21') d=20;
if (a[1]=='22') d=21;
if (a[1]=='23') d=22;
if (a[1]=='24') d=23;
if (a[1]=='25') d=24;
if (a[1]=='26') d=25;
if (a[1]=='27') d=26;
if (a[1]=='28') d=27;
if (a[1]=='29') d=28;
if (a[1]=='30') d=29;
if (a[1]=='31') d=30;
if (d<0) return new Date(d); // Couldn't find the month
return new Date(0,0,0,0,m,a[0],d,a[2]);
}
// This creates the calendar for a given month
function createCalendar(div, month) {
var idOfTextbox = div.getAttribute('datepickertextbox'); // Get the textbox id which was saved in the div
var textbox = document.getElementById(idOfTextbox); // Find the textbox now
var tbl = document.createElement('table');
var topRow = tbl.insertRow(-1);
var td = topRow.insertCell(-1);
var lastYearBn = document.createElement('input');
lastYearBn.type='button'; // Have to immediately set the type due to IE
td.appendChild(lastYearBn);
lastYearBn.value='<<';
lastYearBn.onclick=chooseDate;
lastYearBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()-12,1,0,0,0,0).toString());
var td = topRow.insertCell(-1);
var lastMonthBn = document.createElement('input');
lastMonthBn.type='button'; // Have to immediately set the type due to IE
td.appendChild(lastMonthBn);
lastMonthBn.value='<';
lastMonthBn.onclick=chooseDate;
lastMonthBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()-1,1,0,0,0,0).toString());
var td = topRow.insertCell(-1);
td.colSpan=3;
var mon = document.createElement('input');
mon.type='text';
td.appendChild(mon);
mon.value = getMonthYearString(month);
mon.size=15;
mon.disabled='disabled';
mon.className='monthDsp';
var td = topRow.insertCell(-1);
var nextMonthBn = document.createElement('input');
nextMonthBn.type='button';
td.appendChild(nextMonthBn);
nextMonthBn.value = '>';
nextMonthBn.onclick=chooseDate;
nextMonthBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()+1,1,0,0,0,0).toString());
var td = topRow.insertCell(-1);
var nextYearBn = document.createElement('input');
nextYearBn.type='button'; // Have to immediately set the type due to IE
td.appendChild(nextYearBn);
nextYearBn.value='>>';
nextYearBn.onclick=chooseDate;
nextYearBn.setAttribute('date',new Date(month.getFullYear(),month.getMonth()+12,1,0,0,0,0).toString());
var daysRow = tbl.insertRow(-1);
daysRow.insertCell(-1).innerHTML="Mon";
daysRow.insertCell(-1).innerHTML="Tue";
daysRow.insertCell(-1).innerHTML="Wed";
daysRow.insertCell(-1).innerHTML="Thu";
daysRow.insertCell(-1).innerHTML="Fri";
daysRow.insertCell(-1).innerHTML="Sat";
daysRow.insertCell(-1).innerHTML="Sun";
daysRow.className='daysRow';
// Make the calendar
var selected = parseMyDate(textbox.value); // Try parsing the date
var today = new Date();
date = new Date(month.getFullYear(),month.getMonth(),1,0,0,0,0); // Starting at the 1st of the month
var extras = (date.getDay() + 6) % 7; // How many days of the last month do we need to include?
date.setDate(date.getDate()-extras); // Skip back to the previous monday
while (1) { // Loop for each week
var tr = tbl.insertRow(-1);
for (i=0;i<7;i++) { // Loop each day of this week
var td = tr.insertCell(-1);
var inp = document.createElement('input');
inp.type = 'button';
td.appendChild(inp);
inp.value = date.getDate();
inp.onclick = chooseDate;
inp.setAttribute('date',getDateString(date));
if (date.getMonth() != month.getMonth()) {
if (inp.className) inp.className += ' ';
inp.className+='othermonth';
}
if (date.getDate()==today.getDate() && date.getMonth()==today.getMonth() && date.getFullYear()==today.getFullYear()) {
if (inp.className) inp.className += ' ';
inp.className+='today';
}
if (!isNaN(selected) && date.getDate()==selected.getDate() && date.getMonth()==selected.getMonth() && date.getFullYear()==selected.getFullYear()) {
if (inp.className) inp.className += ' ';
inp.className+='selected';
}
date.setDate(date.getDate()+1); // Increment a day
}
// We are done if we've moved on to the next month
if (date.getMonth() != month.getMonth()) {
break;
}
}
// At the end, we do a quick insert of the newly made table, hopefully to remove any chance of screen flicker
if (div.hasChildNodes()) { // For flicking between months
div.replaceChild(tbl, div.childNodes[0]);
} else { // For creating the calendar when they first click the icon
div.appendChild(tbl);
}
}
// This is called when they click the icon next to the date inputbox
function showDatePicker(idOfTextbox) {
var textbox = document.getElementById(idOfTextbox);
// See if the date picker is already there, if so, remove it
x = textbox.parentNode.getElementsByTagName('div');
for (i=0;i<x.length;i++) {
if (x[i].getAttribute('class')=='datepickerdropdown') {
textbox.parentNode.removeChild(x[i]);
return false;
}
}
// Grab the date, or use the current date if not valid
var date = parseMyDate(textbox.value);
if (isNaN(date)) date = new Date();
// Create the box
var div = document.createElement('div');
div.className='datepickerdropdown';
div.setAttribute('datepickertextbox', idOfTextbox); // Remember the textbox id in the div
createCalendar(div, date); // Create the calendar
insertAfter(div, textbox); // Add the box to screen just after the textbox
return false;
}
// Adds an item after an existing one
function insertAfter(newItem, existingItem) {
if (existingItem.nextSibling) { // Find the next sibling, and add newItem before it
existingItem.parentNode.insertBefore(newItem, existingItem.nextSibling);
} else { // In case the existingItem has no sibling after itself, append it
existingItem.parentNode.appendChild(newItem);
}
}
function onDOMReady(fn,ctx){var ready,timer;var onStateChange=function(e){if(e&&e.type=="DOMContentLoaded"){fireDOMReady()}else if(e&&e.type=="load"){fireDOMReady()}else if(document.readyState){if((/loaded|complete/).test(document.readyState)){fireDOMReady()}else if(!!document.documentElement.doScroll){try{ready||document.documentElement.doScroll('left')}catch(e){return}fireDOMReady()}}};var fireDOMReady=function(){if(!ready){ready=true;fn.call(ctx||window);if(document.removeEventListener)document.removeEventListener("DOMContentLoaded",onStateChange,false);document.onreadystatechange=null;window.onload=null;clearInterval(timer);timer=null}};if(document.addEventListener)document.addEventListener("DOMContentLoaded",onStateChange,false);document.onreadystatechange=onStateChange;timer=setInterval(onStateChange,5);window.onload=onStateChange};
// This is called when the page loads, it searches for inputs where the class is 'datepicker'
onDOMReady(function(){
// Search for elements by class
var allElements = document.getElementsByTagName("*");
for (i=0; i<allElements.length; i++) {
var className = allElements[i].className;
if (className=='datepicker' || className.indexOf('datepicker ') != -1 || className.indexOf(' datepicker') != -1) {
// Found one! Now lets add a datepicker next to it
var a = document.createElement('a');
a.href='#';
a.className="datepickershow";
a.setAttribute('onclick','return showDatePicker("' + allElements[i].id + '")');
var img = document.createElement('img');
img.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAAK/INwWK6QAAABh0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjM2qefiJQAAAdtJREFUOE+Vj+9PUnEUxvPvar3xja96Q1hGEKG0ubZqbfHCNqIVA4eYLAwFp0LYD4iIJEdeRGGZwDAEcUOn9oNIvPcGgjBQfHE69/YFihe1zs59du7d83nOuR0AcOq/CgEqWbaHDqaD+clF1rLAmija6MsZ5vb0s9nB1xm168s9x67y6Y7q2TaXjo8tVKjUTv7Zt61pAkwt/UA3zFwFuxysV2BKAuYeMAnBcBaGukDdCaozaLg5sUGAiQDLA3IIDIBfAfO34N118PaDRwYvRfBcCMrTaLg2liTAOEW3NjzpBZsMpqUwKQaLCMYvwGMhjArQIDfGCTDqy3EAX47lfVTnCo3qCnOzJ8IpW6pJR2IEGHn7/bBaR5MLO8y8CtPuKO2J0nMfGdKr+5uZ4kVdhAD6N99K1bo7ynB5vHpj3AZ0NxWBbs0KAbTur8VKfTbGeFcbkc1sfnBHuA1CzTIB7js/H5SPffFW3q9sau2PDdLhxkl3X+wiQCVYf4Jt3h1Itmb8iBvEusZJd2a2CuXjxXUWU5dSnAZ5/b0QkOobgMKWzh8eMcXaXr6aYSqfcuXtbAkdbS3RfSD/MGDfvGFO9ZuSfY/ilx/GLumi57Vhgfp9W597ECJA2/a/v/4ENLpYKsDo3kgAAAAASUVORK5CYII=';
img.title='Show calendar';
a.appendChild(img);
insertAfter(a, allElements[i]);
}
}
});
那么我对日期做错了什么,导致它在第二天点击日期?示例:用户选择2016年8月18日,但点击日期选择器选择8/19/2016。