简体   繁体   English

jQuery按类别排序的限制数量

[英]jquery sortable limit amount in a list by class

Hi i want to limit the amount of items in my <ul id="sortable1"> where the class is equal to 3 items in that list. 嗨,我想限制我的<ul id="sortable1">中的项目数量,其中该类等于该列表中的3个项目。

I've achieved this when not specific to a class as so: 在不特定于某个类的情况下,我已经实现了这一点:

 $(function() {
    $("#sortable1").sortable({
    connectWith: '.connectedSortable',
    //receive: This event is triggered when a
    //connected sortable list has received an item from another list.
    receive: function(event, ui) {
        // so if > 7
        if ($(this).children().length > 7) {
            //ui.sender: will cancel the change.
            //Useful in the 'receive' callback.
            $(ui.sender).sortable('cancel');
            alert('Your selection has been cancelled. Maximum  7 banners are allowed in the carousel.');

        }
    }
    }).disableSelection();
});

However when I make this more specific this does not work 但是,当我更具体地讲这是行不通的

$('#sortable1').sortable({
    connectWith: ".connectedSortable",

    revert:true,
    receive: function(event, ui)
    {
        var list = $(this);
            if (list.attr('class') = "defaultbanner") {
                    if (list.children().length > 3) {
                            // move item back to main_list
                                        alert('Only three default banners can be selected!');

                            $(ui.sender).sortable('cancel');
                    }
            }
        }
}).disableSelection();

I'm not sure what I am doing wrong, can anyone point me into the right direction? 我不确定自己在做什么错,有人能指出我正确的方向吗?

What I want to do is to not allow more than 3 default banners in my ul sortable1 . 我想做的是在我的ul sortable1不允许超过3个默认横幅。 I've added a class to all my banners called defaultbanners to get a handle on this. 我为我的所有横幅添加了一个名为defaultbanners的类,以defaultbanners进行处理。 The way I've implemented above just doesn't work. 我上面实现的方法不起作用。

Can someone show me how this should be implemented effectively? 有人可以告诉我如何有效实施吗?

My code can be seen on my jsFiddle 我的代码可以在我的jsFiddle上看到

Thanks in advance. 提前致谢。

It can be done like this: 可以这样完成:

receive: function(event, ui)
{
    var list = $(this);
    if (list.children('.defaultbanner').length > 3) {
        // move item back to main_list
        alert('Only three default banners can be selected!');
        $(ui.sender).sortable('cancel');
    }
}

Example: 例:

Fiddle 小提琴

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

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