目前使用我当前的代码处理jquery日期选择器,我可以克隆该行,我可以添加年和月。但是,当我尝试点击添加更多它将再创建一行,并再次我可以添加从日期和日期它完美地计算,但当我点击删除按钮它删除行但不删除相应的值。
这是我厌倦了删除
$(document).on('click', ".btn_less1", function() {
var len = $('.cloned-row3').length;
if (len > 1) {
$(this).closest(".btn_less1").parent().parent().parent().remove();
uppdiff();
}
});
以下是其余代码的fiddle link
提前致谢
答案 0 :(得分:1)
您定义函数的顺序错误,找不到函数uppdiff
,因为它的定义时间很晚。我重新安排了您的代码,如下所示:
$(function() {
var diffDays = 0;
$.datepicker.setDefaults({
dateFormat: "mm/dd/yy",
changeMonth: true,
yearRange: "-100:+0",
changeYear: true,
maxDate: new Date(),
showButtonPanel: false,
beforeShow: function() {
setTimeout(function() {
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
}
});
var valid = true;
function uppdiff() {
if (valid) {
var dateStart = [];
var dateEnd = [];
$.each($('.datepicker'), function() {
if ($(this).hasClass('startDate'))
dateStart.push($(this).datepicker('getDate'))
else
dateEnd.push($(this).datepicker('getDate'))
});
diffDays = 0;
$.each(dateStart, function(key, value) {
$.each(dateStart, function(key2, value) {
if (key !== key2 && !(dateStart[key] >= dateEnd[key2] || dateEnd[key] <= dateStart[key2])) {
alert('Date ranges overlap');
valid = false;
return valid;
}
});
diffDays += parseInt((dateEnd[key] - dateStart[key]) / (1000 * 60 * 60 * 24));
return valid;
});
var year = parseInt(diffDays / 365);
var date = diffDays - (365 * year);
var month = parseInt(date / 30);
//document.getElementById("txt_expy").innerHTML = year + " year";
//document.getElementById("txt_expm").innerHTML = month + " Month";
if(year >= 0){
$("#txt_expy").text(year + " year");
}
if(month >= 0){
$("#txt_expy").text(month + " Month");
}
}
}
$(".cloned-row3").find(".datepicker").removeClass('hasDatepicker').datepicker();
var count = 0;
$(document).on("click", ".exp_add_button", function() {
var $clone = $('.cloned-row3:eq(0)').clone(true, true);
$clone.find('[id]').each(function() {
this.id += 'someotherpart'
});
$clone.find('.btn_more').after("<input type='button' value='Delete Row' class='btn_less1 selbtnless' id='buttonless'/>")
$clone.attr('id', "added" + (++count));
/*$clone.find(".degree_Description").attr('disabled', true).val('');*/
$clone.find("input.startDate,input.endDate")
.val('')
.removeClass('hasDatepicker')
.removeData('datepicker')
.unbind()
.datepicker();
$(this).parents('.wrk_exp').after($clone);
});
$(document).on('change', ".datepicker", function() {
var valid = true;
$.each($('.datepicker'), function() {
if ($(this).val() == "") {
valid = false;
return false;
}
uppdiff();
});
uppdiff();
});
$(document).on('click', ".btn_less1", function() {
var len = $('.cloned-row3').length;
if (len > 1) {
$(this).closest(".btn_less1").parent().parent().parent().remove();
uppdiff();
}
});
});