一个思路
需求是这样的:如下图 (选择加班日期,自动填充签到和签退时间)
![](https://images2018.cnblogs.com/blog/1171033/201808/1171033-20180827100711468-1678767690.jpg)
当点击选择日期按钮的时候,定时器开始监测选择的值,然后去调一个接口取两个时间的值(大致思路就是这样,其中细节(事件兼容性,判断有没有选择日期)见代码 纯纯的js)
function contains(arr, obj) {
if(arr[obj]!='undefined')
return false;
return true;
}
function addEvent(elm, evType, fn, useCapture) {
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
}
}
var listenList = new Array();
document.ready= function (event) {
tdControlClick(event);
var addButton = document.getElementsByClassName("addbtn_p")[0];
addEvent(addButton,'click',function(event){
tdControlClick(event)
})
function tdControlClick(event){
var dateControls = document.getElementsByClassName("calendar");
if (dateControls != "undefined") {
for (var index = 0; index < dateControls.length; index++) {
var element = dateControls[index];
var id = element.getAttribute("id");
if (!contains(listenList, id)) {
listenList[id] = {
id: id,
timer: 0,
date: ''
};
clearInterval(listenList[id].timer);
addEvent(element,'click',function(event){
var event = event || window.event;
var target = event.toElement || event.srcElement;
var objID = event.target.id;
var inter = setInterval(function () {
dateControlClick(objID);
}, 1000);
listenList[objID].timer = inter;
})
}
}
}
}
function dateControlClick(id)
{
var fieldid = id.split('_')[0];
var rowNo = id.split('_')[1].replace('browser', '');
var dateVal = document.getElementById(fieldid + "_" + rowNo + "span").innerText;
if (dateVal != listenList[id].date) {
listenList[id].date = dateVal;
var curdate = listenList[id].date.replace(/\-/,"").replace(/\-/,"");
ajax_methods(curdate,rowNo);
clearInterval(listenList[id].timer);
listenList[id].timer = 0;
}
}
function ajax_methods(date,rowNo) {
var xhr = new XMLHttpRequest();
var personcode =document.getElementById('field12003').value;
xhr.open('post',
'https://zqs.reignwood.com/bi-web/index/extract?indexSetCodes=INDEX171019100002&refresh=1&caller=WorkAttendFromOA'
);
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
var data = {
orgcode: "1",
curdate: date,
type: 3,
personcode: personcode,
personPage: "1",
caller:'WorkAttendFromOA'
}
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var res = JSON.parse(xhr.responseText);
setCheckTime(res.data,rowNo);
}
};
}
function setCheckTime(data,rowNo)
{
document.getElementById('field19209_'+rowNo).value=data.oneday.workStart.toString().substring(0,5);
document.getElementById('field19209_'+rowNo + "span").innerText=data.oneday.workStart.toString().substring(0,5);
document.getElementById('field19210_'+rowNo).value=data.oneday.workEnd.toString().substring(0,5);
document.getElementById('field19210_'+rowNo + "span").innerText=data.oneday.workEnd.toString().substring(0,5);
}
}