简体   繁体   English

JS SlideUp和SlideDown在列表中不起作用

[英]JS SlideUp and SlideDown not working in list

I have created a list dynamically in JS, with a provision to Add/Delete items. 我已经在JS中动态创建了一个列表,并提供了添加/删除项目的功能。 If I Delete an item from the end of the list, then subsequent Add works fine. 如果我从列表末尾删除一个项目,那么后续添加工作正常。 However, if I delete an item within (not last element), then the subsequent Added item is not visible. 但是,如果删除其中的一个项目(不是最后一个元素),则看不到后续的“已添加”项目。 I'm not sure I got my understanding of SlideUp, SlideDown correctly. 我不确定我是否正确理解了SlideUp,SlideDown。 Can someone please check the code and let me know where I'm wrong. 有人可以检查代码,让我知道我哪里错了。

 function addNewInSection() { var imgCntr; var path; var fpath; var desc; var secListData = ''; selectedImageList.push.apply(selectedImageList, localImageList); localImageList = []; // After copying, reset local list var section = removeSpecials($('#pa-section-list > .active').find('a').find('#section-name').text().trim()); var count = $('#pa-section-list-data').find('#' + section).find('.list-group').find('.list-group-item').length; var id; for (imgCntr = count; imgCntr < selectedImageList.length; imgCntr++) { path = selectedImageList[imgCntr][0].trim(); fpath = selectedImageList[imgCntr][1]; desc = selectedImageList[imgCntr][2]; id = section + '-' + imgCntr; secListData = '<div style="display: none;" id="' + id + '" class="list-group-item list-item-height clearfix">' + '<img height="50" src="' + path + '" data-fpath="' + fpath + '">' + '<label id="imgdesc">' + desc + '</label>' + '<button on`click="upListItem(\\'' + id + '\\')" class="btn btn-sm btn-info pull-right"><span class="fa fa-arrow-up"></span></button>' + '<button onclick="downListItem(\\'' + id + '\\')" class="btn btn-sm btn-info pull-right"><span class="fa fa-arrow-down"></span></button>' + '<button onclick="deleteListItem(\\'' + id + '\\', \\'' + imgCntr + '\\')" class="btn btn-sm btn-warning pull-right"><span class="fa fa-trash"></span></button>' + '<button onclick="addImageDescription(\\'' + imgCntr + '\\');" class="btn btn-sm btn-info pull-right"><span class="fa fa-edit"></span></button>' + '</div>'; $('#pa-section-list-data').find('#' + section).find('.list-group').append(secListData); $('#' + id).slideDown(); count = $('#pa-section-list-data').find('#' + section).find('.list-group').find('.list-group-item').length; } recordSections(); // Send to server } function deleteListItem(id, imgcnt) { bootbox.confirm("Do you really want to delete?", function (result) { if (result) { $('#' + id).slideUp("slow", function () { selectedImageList.splice(imgcnt, 1); $('#' + id).remove(); recordSections(); }); } }); } 

I believe that's because your script might generate same id, while jQuery's Id selector can only select the first Id found. 我认为这是因为您的脚本可能会生成相同的ID,而jQuery的ID选择器只能选择找到的第一个ID。 You don't have to use Id for your object selector, instead, use this parameter as a reference. 您不必使用Id作为对象选择器,而是使用this参数作为参考。

You can change this line : 你可以改变这一行:

secListData = '<div style="display: none;" id="' + id + '" class="list-group-item list-item-height clearfix">'
                + '<img  height="50" src="' + path + '" data-fpath="' + fpath + '">'
                + '<label id="imgdesc">' + desc + '</label>'
                + '<button on`click="upListItem(\'' + id + '\')" class="btn btn-sm btn-info pull-right"><span class="fa fa-arrow-up"></span></button>'
                + '<button onclick="downListItem(\'' + id + '\')" class="btn btn-sm btn-info pull-right"><span class="fa fa-arrow-down"></span></button>'
                + '<button onclick="deleteListItem(\'' + id + '\', \'' + imgCntr + '\')" class="btn btn-sm btn-warning pull-right"><span class="fa fa-trash"></span></button>'
                + '<button onclick="addImageDescription(\'' + imgCntr + '\');" class="btn btn-sm btn-info pull-right"><span class="fa fa-edit"></span></button>'
                + '</div>';

$('#pa-section-list-data').find('#' + section).find('.list-group').append(secListData);
        $('#' + id).slideDown();

into this one : 进入这个:

// create element from jQuery
secListData = $('<div style="display: none;" class="list-group-item list-item-height clearfix">'
                + '<img  height="50" src="' + path + '" data-fpath="' + fpath + '">'
                + '<label id="imgdesc">' + desc + '</label>'
                + '<button on`click="upListItem(this)" class="btn btn-sm btn-info pull-right"><span class="fa fa-arrow-up"></span></button>'
                + '<button onclick="downListItem(this)" class="btn btn-sm btn-info pull-right"><span class="fa fa-arrow-down"></span></button>'
                + '<button onclick="deleteListItem(this)" class="btn btn-sm btn-warning pull-right"><span class="fa fa-trash"></span></button>'
                + '<button onclick="addImageDescription(this);" class="btn btn-sm btn-info pull-right"><span class="fa fa-edit"></span></button>'
                + '</div>');

$('#pa-section-list-data').find('#' + section).find('.list-group').append(secListData);
// you can still access the object like this without id
secListData.slideDown();

and for your delete function change to this : 并为您的删除功能更改为:

function deleteListItem(elm) {
    // get parent element
    var parentElm = $(elm).closest('.list-group-item');
    bootbox.confirm("Do you really want to delete?", function (result) {
        if (result) {
            parentElm.slideUp("slow", function () {
                parentElm.remove();
                // do other things here
            });
        }
    });
}

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM