我有一个记录来自不同员工的数据的页面。其中一件事就是记录它们的进入和退出。我可以使用localStorage记录这些数据,但这适用于所有人。试图使用localStorage项目的不同名称,但是当我更改员工时它不起作用。有人可以帮我完成这个吗? 这是所有相同的代码:
var entradasaida = 0;
entradasaida = localStorage.getItem("entradasaida");
if(entradasaida == 0){
var time = $('#time').val();
var time2 = $('#time2').val();
var datetime = new Date();
var year = datetime.getFullYear();
var month = datetime.getMonth()+1;
if(month < 10){
month = "0"+month
}
var day = datetime.getDay()+1;
if(day < 10){
day = "0"+day
}
var date = new Date(year+"-"+month+"-"+day+" "+time+":"+time2);
var mills = date.getTime();
var timeE = localStorage.getItem("horas");
var time2E = localStorage.getItem("mins");
alert(timeE);
localStorage.setItem("horas", time);
localStorage.setItem("mins", time2);
localStorage.setItem("entradasaida",1);
localStorage.setItem("millsEnt",mills);
if(timeE != null){
$('#horas_button').text("Saida");
$('#time, #time2').val("");
$('#entrada').text(timeE+":"+time2E);
}
} else{
var horasEnt = localStorage.getItem("horas");
var minsEnt = localStorage.getItem("mins");
var entrada = localStorage.getItem("millsEnt");
localStorage.setItem("entradasaida",0);
$('#horas_button').text("Entrada");
var time = $('#time').val();
var time2 = $('#time2').val();
var datetime = new Date();
var year = datetime.getFullYear();
var month = datetime.getMonth()+1;
if(month < 10){
month = "0"+month
}
var day = datetime.getDay()+1;
if(day < 10){
day = "0"+day
}
var date1 = new Date(year+"-"+month+"-"+day+" "+time+":"+time2);
var millssai = date1.getTime();
$('#millssai').text(millssai);
var totMills = (millssai - entrada)
var horas = time - horasEnt;
var mins = time2 - minsEnt;
if(mins < 0){
mins = mins*(-1);
mins = mins.toFixed(2);
horas = horas-1;
}
var totHoras = horas+":"+mins
$('#totHoras').text(totHoras);
$('#mills').text(totMills);
$('#time, #time2').val("");
$('#entrada').text(totHoras);
}
};
如果我添加此变量var func = $("#func option:selected").text();
并将func
添加到所有本地存储项,则会导致所有内容混乱。
答案 0 :(得分:0)
<强>解强> 终于搞定了。这是完整的代码:
var func= "";
var entradasaida = 0;
$( "#func" ).change(function() {
func = $("#func option:selected").text();
entradasaida = localStorage.getItem(func+"entradasaida");
if(entradasaida == 1){
$('#horas_button').text("Saida");
var horasEnt = localStorage.getItem(func+"horas");
var minsEnt = localStorage.getItem(func+"mins");
$('#entrada').text(horasEnt+":"+minsEnt);
} else{
$('#horas_button').text("Entrada");
$('#entrada').text("Horário");
}
});
$(".inputs").keyup(function () {
if (this.value.length == this.maxLength) {
$(this).next('.inputs').focus();
}
});
$(".inputs1").keyup(function () {
if (this.value.length == this.maxLength) {
$(this).next('.inputs').focus();
}
});
var callback = function() {
alert(func);
entradasaida = localStorage.getItem(func+"entradasaida");
if(entradasaida == 0){
var time = $('#time').val();
var time2 = $('#time2').val();
var datetime = new Date();
var year = datetime.getFullYear();
var month = datetime.getMonth()+1;
if(month < 10){
month = "0"+month
}
var day = datetime.getDay()+1;
if(day < 10){
day = "0"+day
}
var date = new Date(year+"-"+month+"-"+day+" "+time+":"+time2);
var mills = date.getTime();
var timeE = localStorage.getItem(func+"horas");
var time2E = localStorage.getItem(func+"mins");
alert(timeE);
localStorage.setItem(func+"horas", time);
localStorage.setItem(func+"mins", time2);
localStorage.setItem(func+"entradasaida",1);
localStorage.setItem(func+"millsEnt",mills);
$('#horas_button').text("Saida");
$('#time, #time2').val("");
$('#entrada').text(timeE+":"+time2E);
} else{
var horasEnt = localStorage.getItem(func+"horas");
var minsEnt = localStorage.getItem(func+"mins");
var entrada = localStorage.getItem(func+"millsEnt");
localStorage.setItem(func+"entradasaida",0);
var time = $('#time').val();
var time2 = $('#time2').val();
var datetime = new Date();
var year = datetime.getFullYear();
var month = datetime.getMonth()+1;
if(month < 10){
month = "0"+month
}
var day = datetime.getDay()+1;
if(day < 10){
day = "0"+day
}
var date1 = new Date(year+"-"+month+"-"+day+" "+time+":"+time2);
var millssai = date1.getTime();
$('#millssai').text(millssai);
var totMills = (millssai - entrada)
var horas = time - horasEnt;
var mins = time2 - minsEnt;
if(mins < 0){
mins = mins*(-1);
mins = mins.toFixed(2);
horas = horas-1;
}
var totHoras = horas+":"+mins
$('#totHoras').text(totHoras);
$('#mills').text(totMills);
$('#time, #time2').val("");
$('#entrada').text(totHoras);
$('#horas_button').text("Entrada");
}
};
$("horas_button").keypress(function() {
callback();
});
$('#horas_button').click(callback);