Clone div and rename element ids incrementally

夙愿已清 提交于 2019-12-06 10:05:13

From your code I think your main problem is accessing the group of dropdowns uniquely without conflict. If this is the case, I think that could be achieved without the stress of incrementing the id's of each element for every group created. If I were to do this, I would approach it as follows.

First the DOM (an example):


<div id="card">
    <div class="group">
        <select id="country" name="country[]">
            <option>select</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>
        <select id="filter" name="filter[]">
            <option>select</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>
    </div>
</div>
<a id="more" href="">More</a>

Then the jquery:


$(function(){
    var newgroup = $('<div>').addClass('group');
    $('#more').click(function(e){
        e.preventDefault();
        $('.group').first().clone().appendTo(newgroup).appendTo('#card');
    });

    $('.group #country').live('change',function(){
        $(this).parent().find('#filter').val(1);
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!