问题描述
当前正在工作的jQuery克隆,其中我使用当前代码Increment正在整个div的id递增,但是在expy -1序列中不起作用,如果用户单击添加更多按钮,它将生成随机id编号expy-3.所以有一次,当我添加行时,我想用我的代码删除行,我可以删除行,但是id不会递减,例如如果我有expy-1,expy-2 expy-3,如果我删除expy-2 expy-3 id必须更改expy-2,因为我已经拥有expy1
currently working jQuery clone where I am increment id for the entire div with my current code Increment was working but not in sequence expy -1 and if user click add more button it generating random id number expy-3. So once when I add the row I want to delete the row with my code i can able to delete the row but the id was not decrementing for example if i have expy-1, expy-2 expy-3 if i delete expy-2 the expy-3 id has to change expy-2 because already i had expy1
这是jquery代码
$(document).on("click", ".exp_add_button", function() {
var $clone = $('.cloned-row3:eq(0)').clone(true, true);
var num = $('.cloned-row3').length;
$clone.find(".chk_Field_exp").val('');
$clone.find('[id]').each(function() {
this.id += '_' + num;
$(this).removeClass("errRed");
if ($(this).hasClass("required_Field")) {
$(this).prevAll('label').find('span.required-star').removeClass('text-error-red');
$(this).addClass("cloned_field");
} else {
$(this).removeClass("errRed");
$(this).removeClass("text-error-red");
}
});
$clone.find('.btn_more').after("<input type='button' class='btn_less1 edu_btnle' id='buttonless" + (++rowCount) + "'/>")
$clone.attr('id', "expy-" + (++rowCount)).addClass('exp_add');
$clone.find(".startDate").val('');
$clone.find(".endDate").val('');
/*$clone.find(".degree_Description").attr('disabled', true).val('');*/
$clone.find(".startDate,.endDate")
.removeClass('hasDatepicker')
.removeData('datepicker')
.datepicker({
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);
}
});
$(this).parents('.wrk_exp').after($clone);
});
这是删除代码
$(document).on('click', ".btn_less1", function() {
var len = $('.cloned-row3').length;
if (len > 1) {
$(this).closest(".btn_less1").parent().parent().parent().remove();
}
});
这是小提琴链接
预先感谢
推荐答案
这是因为在以下两行中您将rowCount
的值增加了两次:
This is because you are increasing the value of rowCount
twice, in these two line:
$clone.find('.btn_more').after("<input type='button' class='btn_less1 edu_btnle' id='buttonless" + (++rowCount) + "'/>")
$clone.attr('id', "expy-" + (++rowCount)).addClass('exp_add');
从底部rowCount
删除一个++
,代码为:
Remove one ++
from the bottom rowCount
, code will be:
$clone.find('.btn_more').after("<input type='button' class='btn_less1 edu_btnle' id='buttonless" + (++rowCount) + "'/>")
$clone.attr('id', "expy-" + (rowCount)).addClass('exp_add');
++rowCount
更改rowCount
的值并等于
rowCount = rowCount+1
这篇关于jQuery克隆增量ID不在序列中,也用于删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!