本文介绍了JQuery克隆序列没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是以前的 SO 问题,我可以看到连续数字的当前代码如果我确实喜欢这个最初我的ID以expy -1开头,如果我点击添加更多按钮,它会完美地expy-2但是当我点击expy-1时,expy-3克隆必须在expy-2之后才会出现在expy-1和expy-2之内 当我尝试删除expy-2从那里它必须自动检测ID 例如,如果我有expy-1,expy-2,expy-3& expy-4如果我删除expy2最终结果应该是expy-1,expy-2,expy-3 按照前面的用户告诉我已经删除了 ++ rowCount 并更改为 rowcount this.id + ='_'+ num; console.log(this.id) ; $(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); } }); $('。btn_more')。after(< input type ='button'class ='btn_less1 edu_btnle'id ='buttonless+(++ rowCount)+'/> ) $ clone.attr('id',expy-+(rowCount))。addClass('exp_add'); 以下是 fiddle link 解决方案 在点击 btn_less1 allot colned-row3 新 ids $(document).on('click',.btn_less1,function(){ var len = $('。cloned-row3')。length; if(len> 1){ $(this).closest(。btn_less1)。closest(。cloned-row3 ).remove(); } //遍历克隆的row3和changin的id $(.cloned-row3)。each(function(i){ this.id =expy - +(i + 1); }); }); 现场小提琴 有意义吗? This is the continuation with previous SO question with this current code where I can able to see consecutive number If i did like this initially my id starts with expy -1, if i click add more button it was coming perfectly expy-2 but when i click from expy -1 the expy-3 clone has to come after expy-2 not inside expy-1 and expy-2And when I try to delete expy-2 from there it has to automatically detect the idfor example if i have expy-1,expy-2,expy-3 & expy-4 if i delete expy2 the final result should come as expy-1,expy-2,expy-3As per the Previous user told i have removed ++rowCount and changed to rowcount$clone.find('[id]').each(function() { this.id += '_' + num; console.log(this.id); $(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');Here is the fiddle link 解决方案 On click of btn_less1 allot colned-row3 new ids$(document).on('click', ".btn_less1", function() { var len = $('.cloned-row3').length; if (len > 1) { $(this).closest(".btn_less1").closest(".cloned-row3").remove(); } // Iterating cloned-row3 and changin the id $(".cloned-row3").each(function(i){ this.id = "expy-"+(i+1); }); });Live FiddleMakes sense? 这篇关于JQuery克隆序列没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 14:12