我有一个通过ajax自动保存数据的表单。 特别是我有一个专栏(名为start_date),它出现了员工休假的开始日期。这是一个输入,其默认值为当前日期。此输入附近存在日历(按钮)输入。当用户从此日历中选择日期时,此值也会保存在数据库中。这些值通过ajax自动保存而不提交按钮。还存在一个添加新行的按钮。我的问题是需要刷新页面才能显示日历,当我点击日期时,日历会再次消失。 我将此代码用于datepicker:
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='calendar.png';
img.style.height='17px';
img.style.width='17px';
img.title='Show calendar';
a.appendChild(img);
insertAfter(a, allElements[i]);
}
}
});
Ajax代码:
function update_record (evt,elem,table,field,check_field,check_var) {
if (elem.tagName=="INPUT") {
elem.value=capitalizeFirstLetter(elem.value);
}
document.getElementById('test_texts').innerHTML='worked';
var elem_id=elem.id;//store id to find the elem later in case a screen refresh has happened.
var val=elem.value;
if (field=="start_date" || field=="end_date") {
var date_split=elem.value.split('-');
val=date_split[2]+"-"+date_split[1]+"-"+date_split[0];
}
if (elem.style.backgroundColor=="yellow") {//update only if there has been a change
loadXMLDoc("/prosopiko/update_record.php?table="+encodeURIComponent(table)+"&field="+encodeURIComponent(field)+"&val="+encodeURIComponent(val)+"&check_field="+encodeURIComponent(check_field)+"&check_var="+encodeURIComponent(check_var),function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
if (table=='posto') {
document.getElementById('kathikoda_edit').innerHTML=xmlhttp.responseText;
}
document.getElementById(elem_id).style.backgroundColor="lightgreen";
document.getElementById('test_texts').innerHTML=xmlhttp.responseText;
}
})
}
}
此链接Datepicker Calendar appears after refresh。关于如何保持datepicker按钮不变的任何建议?