简体   繁体   中英

Prevent listbox from scrolling to the item getting selected (with jquery)

I have a listbox, with items that can be in some kind of group with other items. When one item is selected, all items from the group are selected with jquery.

Its really annoying that the listbox scrolls down to the items that jquery is selecting. I want to stay at the position of the item selected by the user.

So how can i prevent the listbox from scrolling down when items get selected?

jsfiddle example here: example

EDIT: click on item number 10 in the example, and he goes to 78, thats the issue here.

You can try this:

$('#lb').change(function() { 

    var x = $(this).scrollTop();

    $('#lb option:selected').each(function() { 
        var groupName = $(this).attr('group'); 
        if (groupName !== undefined) { 
            $('#lb option[group=' + groupName + ']').each(function() { 
                this.selected = true; 
            }); 
        }

    }); 

     $(this).scrollTop(x); 
} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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