防止列表框滚动到被选中的项目(使用jquery)

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

本文关键字:项目 使用 jquery 列表 滚动      更新时间:2023-09-26

我有一个列表框,其中的项目可以与其他项目在某种类型的组中。当一个项目被选中时,该组中的所有项目都会被jquery选中。

列表框向下滚动到jquery正在选择的项目,这真的很烦人。我想停留在用户选择的项目的位置。

那么,当项目被选中时,我如何防止列表框向下滚动?

jsfield example: example

编辑:点击示例中的第10项,他转到第78项,这就是问题所在。

你可以试试:

$('#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); 
} );