jquery 在此元素之后插入克隆时元素丢失事件

a2mppw5e  于 2022-12-12  发布在  jQuery
关注(0)|答案(2)|浏览(201)

我在id="coclevel1"的页面上有一个下拉列表()。如果当前选中的选项有子项,那么我必须添加新的下拉列表,并用子项填充它。下一个下拉列表应该命名为“coclevel2”,“coclevel3”等。我必须为每个添加的下拉列表执行此事件。在$(document).ready中,我放置了:

$('[id^=coclevel]').live('change', function() {
    var currElement = $(this).attr('id');
    alert(currElement); //for debug
    cleanUpCocOptionsAfter(currElement);
    renderSubItemsOf(currElement);
});

renderSubItemsOf(currElement);查询DB,如果数据不为空,则添加新的下拉列表并填充。

function cleanUpCocOptionsAfter(currElement) {
    var num = $('.cocItems').length;
    var currNum = parseInt(currElement.substring(8, currElement.length));
    for (var i = currNum + 1; i <= num; i++) {
        $('#coclevel' + i).remove();
        $('#cocBr' + i).remove();
    }
}

function renderSubItemsOf(currElement) {
    var parentId = $('#' + currElement + ' option:selected').val();
    $.getJSON('getchildrenof/' + parentId,
            function() {

            }).success(function(data) {
                var len = data.length;
                if (len > 0) {
                    var newElemSelector = '#' + addCocOptionAfter(currElement);
                    for (var i = 0; i < len; i++) {
                        $(newElemSelector).append($("<option></option>").attr("value", data[i].id).text(data[i].description));
                    }
                } else {
                    //not developed yet
                }
            });
}

function addCocOptionAfter(currElement) {
    // how many "duplicatable" input fields we currently have
    var num = $('.cocItems').length;
    var currNum = parseInt(currElement.substring(8, currElement.length));
    var currElemSelector = '#' + currElement;
    // the numeric ID of the new input field being added
    var newNum = new Number(num + 1);

    // create the new element via clone(),
    //and manipulate it's ID using newNum value
    var newElemId = 'coclevel' + newNum;

    var newElem = $(currElemSelector).clone().attr('id', newElemId).empty();
    $(newElem).append($("<option></option>").attr("value", -1).text('-- Select one --'));

    // insert the new element after the last "duplicatable" input field
    $(currElemSelector).after(newElem);
    $(currElemSelector).after('<br/>').attr('id', 'cocBr' + newNum);

    return newElemId;
}

添加新的下拉框和填充工作正常,但此后以前的下拉框丢失事件,当我试图改变所选选项没有出现(浏览器不显示alert(currElement); //for debug)。请帮助解决这个问题。

pod7payv

pod7payv1#

我不确定我是否理解您的要求,但是如果您希望jQuery事件处理程序与一个元素一起被克隆,那么您需要使用.clone(true)而不仅仅是clone()

pinkon5k

pinkon5k2#

这里不需要使用数字id,通过传递DOM对象本身可以更有效地实现这一点:

$('.cocItems').live('change', function() {
    cleanUpCocOptionsAfter(this);
    renderSubItemsOf(this);
});

function cleanUpCocOptionsAfter(currElement) {
    $(currElement).nextAll('.cocItems').remove();
}

function renderSubItemsOf(currElement) {
    var parentId = $(currElement).val();
    $.getJSON('getchildrenof/' + parentId, function(data) {
        var len = data.length;
        if (len > 0) {
            addCocOptionAfter(currElement, data)
        } else {
            //not developed yet
        }
    });
}

function addCocOptionAfter(currElement, data) {
    var newElem = $('<select />').addClass('cocItems');
    $('<option />')
        .attr('value', -1)
        .text('-- Select one --')
        .appendTo(newElem);

    $.each(data, function() {
         $('<option />')
             .attr('value', this.id)
             .text(this.description)
             .appendTo(newElem);
    });
    newElem.insertAfter(currElement);
    return newElem;
}

相关问题