2015-12-21 41 views
0

當前正在工作的jQuery克隆我在哪裏增加我的當前代碼整個div的ID遞增工作,但不按順序expy -1,如果用戶點擊添加更多按鈕它會生成隨機ID號碼expy-3。所以一旦我添加行我想刪除行與我的代碼我可以刪除行,但ID不是遞減,例如,如果我有expy-1,expy-2 expy-3如果我刪除expy-2所述EXPY-3 ID具有改變EXPY-2,因爲已經我有expy1JQuery克隆增量ID不是即將到來的序列和刪除行也

這裏是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(); 
} 

})的代碼;

這裏是fiddle link

在此先感謝

+0

你真的需要的ID在這裏?我想沒有 –

回答

1

這是因爲你增加rowCount值的兩倍,在這兩個行:

$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刪除一個++ ,代碼將爲:

$clone.find('.btn_more').after("<input type='button' class='btn_less1 edu_btnle' id='buttonless" + (++rowCount) + "'/>") 
$clone.attr('id', "expy-" + (rowCount)).addClass('exp_add'); 

Fiddle

++rowCount改變的rowCount的價值,等於

rowCount = rowCount+1 
+0

謝謝你的回覆我有一個問題,例如,如果我加5行和1,2,3,4,5,如果我刪除行3目前它是1,2,4,5但它應該來作爲1,2,3,4請親自幫助我 – Mahadevan

+0

這回答了問題的一部分。當你刪除id不會減少。 – rottenoats

+0

@KФ我不能接受,請你解釋一下更多:( – Mahadevan