jquery - Select2 在每次选择时将当前选择范围滚动到底部(closeOnSelect = False)
问题描述
JSFiddle:http: //jsfiddle.net/xpvt214o/776660/
我有一个固定高度为 1 行的多选select2
控件。当我选择新的多个项目时,它们会被添加,但跨度不会将光标移动到“当前”框的末尾。当前选择器卡在当前行。一旦我离开选择过程,它只会移动到最后。
我尝试了这段代码,您可以对其进行评论以查看原始问题:
/* Scroll CurrSel SPAN to bottom on every Select2 Select event */
/* Comment/Toggle this ON/OFF for testing */
$('#dropdown').on("select2:selecting", function(e) {
var currselspan = $('#dropdown').next().find('.select2-selection--multiple').first();
console.log('scrollTop = ' + $(currselspan).scrollTop() + ' scrollHeight = ' + $(currselspan).prop('scrollHeight'));
$(currselspan).scrollTop($(currselspan).prop('scrollHeight'));
});
这是一个改进,但仍然存在一个问题:新条目第一次进入下一行时,当前选择范围不会滚动。它仅在换行符进入后才开始滚动。
目标:任何时候“换行条目进入”时,都会自动滚动到当前选择的末尾。
解决方案
只需在您的方法中添加以下行。
$(".select2-selection--multiple input").focus();
所以你的方法将是
$('#dropdown').on("select2:selecting", function(e) {
var currselspan = $('#dropdown').next().find('.select2-selection--multiple').first();
console.log('scrollTop = ' + $(currselspan).scrollTop() + ' scrollHeight = ' + $(currselspan).prop('scrollHeight'));
$(currselspan).scrollTop($(currselspan).prop('scrollHeight'));
$(".select2-selection--multiple input").focus();
});
你可以在这里测试它.. http://jsfiddle.net/xpvt214o/776765/
推荐阅读
- python - 如何从 perlin-noise 获得 2D 瓦片输出?
- git - 有没有办法孤立从稀疏结帐创建的分支?
- python - 如何得到“\v”之后的前一句?
- javascript - 如何始终将 mat-menu 保持在同一位置?
- sql - 在一次基于加入条件的 SQL 面试中,我被问到以下伪代码。谁能解释我的方法出了什么问题?
- javascript - 来自 Image 的 Chromium createObjectUrl 以 net::ERR_FILE_NOT_FOUND 的形式出现
- python-3.x - Persistent ModuleNotFoundError: No module named after setting path to project on Windows OS
- python - Python pandas 从 GitHub 读取 gzipped csv
- node.js - 将mongo查询转换为聚合查询?
- c# - 带有子查询的 SQL Server 查询到 LINQ 查询