目前正在jquery克隆和datepicker工作,它將計算日期,它將檢查重疊日期。但是,如果點擊less按鈕,代碼完美工作,它將刪除行,但它沒有檢測到克隆的div中的日期。例如,當用戶選擇日期1990年10月1日 & 1995年10月1日這個總在克隆DIV 總工作經驗5年0月 &如果用戶給10-01 -1996 & 10-01-2015 so總共有工作經驗25年0個月。如果用戶點擊lessbtn按鈕,克隆的按鈕將被刪除,但總年份未被檢測到。使用jquery日期選取器減去日期
這裏是jQuery代碼
$(document).on('change', ".datepicker", function(){
var valid=true;
$.each($('.datepicker'),function(){
if($(this).val()=="")
{
valid=false;
return false;
}
});
if(valid)
{
var dateStart=[];
var dateEnd=[];
$.each($('.datepicker'),function(){
if($(this).hasClass('startDate'))
dateStart.push($(this).val())
else
dateEnd.push($(this).val())
});
$.each($(dateStart),function(key,value){
var x = dateStart[key].split("-");
var y = dateEnd[key].split("-");
var failed = false;
var fromdate = new Date(x[2], x[0] - 1, x[1]);
var todate = new Date(y[2], y[0] - 1, y[1]);
var locDiffDays = parseInt((todate.getTime() - fromdate.getTime())/(1000 * 60 * 60 * 24));
console.log(x);
console.log(y);
console.log(fromdate);
console.log(todate);
console.log(locDiffDays);
if(locDiffDays<0){
alert("To date " + dateEnd[key] + " should be greater then from date " + dateStart[key]);
console.log("invalid from and to dates"); failed = true; return false;
}
if(dateStart[key-1]){
var x1 = dateStart[key-1].split("-");
var y1 = dateEnd[key-1].split("-");
var fromdate1 = new Date(x1[2], x1[0] - 1, x1[1]);
var todate1 = new Date(y1[2], y1[0] - 1, y1[1]);
var locDiffDays1 = parseInt((todate1.getTime() - fromdate1.getTime())/(1000 * 60 * 60 * 24));
console.log(x1);
console.log(y1);
console.log(fromdate1);
console.log(todate1);
console.log(locDiffDays1);
var locDiffDays2 = parseInt((fromdate.getTime() - fromdate1.getTime())/(1000 * 60 * 60 * 24));
var locDiffDays3 = parseInt((todate.getTime() - todate1.getTime())/(1000 * 60 * 60 * 24));
var locDiffDays4 = parseInt((fromdate.getTime() - todate1.getTime())/(1000 * 60 * 60 * 24));
console.log("locDiffDays2: " + locDiffDays2);
if(locDiffDays2<0){
alert("From date " + dateStart[key] + " should be greater than previous from date " + dateStart[key-1]);
console.log("invalid from dates"); failed = true; return false;
}
if(locDiffDays3<0){
alert("To date " + dateStart[key] + " should be greater than previous To date " + dateStart[key-1]);
console.log("invalid from dates"); failed = true; return false;
}
if(locDiffDays4<0){
alert("From date " + dateStart[key] + " should be greater than previous To date " + dateEnd[key-1]);
console.log("invalid from dates"); failed = true; return false;
}
}
if(key == dateStart.length-1 && !failed){
var firstDate = dateStart[0].split('-');
firstDate = new Date(firstDate[2], firstDate[0] - 1, firstDate[1]);
var lastDate = dateEnd[dateEnd.length-1].split('-');
lastDate = new Date(lastDate[2], lastDate[0] - 1, lastDate[1]);
console.log(lastDate);
console.log(firstDate);
//var diffYears = (lastDate.getTime() - firstDate.getTime())/(1000 * 60 * 60 * 24 * 365);
var diffMonths = monthDiff(firstDate, lastDate);
//diffYears = parseInt(''+diffYears);
var diffYears = diffMonths/12;
diffYears = parseInt(''+diffYears);
diffMonths = diffMonths - (diffYears * 12)
document.getElementById("txt_expy").innerHTML = diffYears + " years";
document.getElementById("txt_expm").innerHTML = diffMonths + " Months";
}
});
請請建議我。
我很困惑在這裏,我想要把任何計數或東西,所以當用戶點擊少按鈕計數應該減少一個,應該檢測到的值。請給我提示以下
Hereis小提琴Link
感謝&問候
任何建議,請 – Mahadevan