简体   繁体   English

Chrome中多选列表上的jQuery设置道具(“已选”,真)只能使用一次

[英]jQuery setting prop(“selected”, true) on multi select list in Chrome only works once

When trying to set a select list option to selected in order to scroll the select list to that option , it works in all browsers except for Chrome. 尝试将选择列表选项设置为选中以将选择列表滚动到该选项时 ,它适用于除Chrome之外的所有浏览器。 In Chrome it works once , but successive times do not work in Chrome. 在Chrome中它可以运行一次 ,但连续几次在Chrome中无效。 How can I ensure that setting the selected attribute of an option in a select list will scroll that option into view? 如何确保在选择列表中设置选项的选定属性会将该选项滚动到视图中?

Here is an example of my issue - http://jsfiddle.net/Z2rQG/ 这是我的问题的一个例子 - http://jsfiddle.net/Z2rQG/

The code I am using to select an option in a list to scroll it into to view is as follows: 我用来选择列表中的选项以将其滚动到视图的代码如下:

(function ($) {
    $.fn.scrollToOption = function (option) {
        var _option = $(option, this);
        // if option is in list
        if(_option) {
            // store the selection state
            var isSelected = _option.is(":selected");

            _option.prop("selected", true); // scroll to the option
            _option.prop("selected", isSelected);  // restore the selection state
        }
        return this;
    };
})(jQuery); 

Edit: I have also tried the scrollTo jQuery plugin, which does not work as well in Chrome. 编辑:我也尝试过scrollTo jQuery插件,这在Chrome中不起作用。 Here is an example - http://jsfiddle.net/kavun/TW4XK/ 这是一个例子 - http://jsfiddle.net/kavun/TW4XK/

Edit: Here is a clearer example of what I am trying to do. 编辑:这是我想要做的更清楚的例子。 Select two options from a select list and have the list be scrolled to the last selected option. 从选择列表中选择两个选项,并将列表滚动到最后选择的选项。 This works in all browsers except Chrome and Safari. 这适用于除Chrome和Safari之外的所有浏览器。 In Chrome the first selection scrolls to the option but the second $('#select option[value=""].prop('selected', true); does not scroll the list - http://jsfiddle.net/kavun/uhnZH/ 在Chrome中,第一个选项滚动到选项但第二个$('#select option[value=""].prop('selected', true); 不滚动列表 - http://jsfiddle.net/kavun/ uhnZH /

Browser behaviour is inconsistent with regard to which option is scrolled into view when multiple options are selected. 当选择多个选项时,浏览器行为与滚动到视图中的选项不一致。 However, selecting the option using its selected property plus setting the selectedIndex property of the <select> element seems to make sure the select is scrolled to the right place in all major browsers. 但是,使用其selected属性选择选项以及设置<select>元素的selectedIndex属性似乎可以确保选择在所有主流浏览器中滚动到正确的位置。 Using this, you can get the correct vertical scrolling, select the options you want and then scroll the select manually. 使用此选项,您可以获得正确的垂直滚动,选择所需的选项,然后手动滚动选择。

Opera does not seem to fully support scrollTop for select elements, so this solution does not work in Opera. Opera似乎并不完全支持选择元素的scrollTop ,因此这个解决方案在Opera中不起作用。

Demo: http://jsfiddle.net/uhnZH/10/ 演示: http//jsfiddle.net/uhnZH/10/

Code: 码:

function selectAndScrollToOption(select, option) {
    $select = $(select);

    // Store the currently selected options
    var $selectedOptions = $select.find("option:selected");

    // Select the new option using its selected property and selectedIndex.
    // This seems to make the select scroll to the desired place in all major
    // browsers
    option.selected = true; // Required for old IE
    select.selectedIndex = option.index;

    // Measure the vertical scrolling
    var scrollTop = $select.scrollTop();

    // Re-select the original options
    $selectedOptions.prop("selected", true);

    // Scroll to the correct place
    $select.scrollTop(scrollTop);
}

If your goal is simply to scroll to a value in the dropdown, you can simply use val as demonstrated in this updated fiddle : 如果您的目标只是滚动到下拉列表中的值,您可以简单地使用val ,如此更新的小提琴中所示

$('#selectList').val(51);

That will work in any browser. 这适用于任何浏览器。

If you're trying to scroll to an element on the page based on the value in the dropdown, you'll have to put up an example that gets us a bit closer to what you want so we can address that specific scenario. 如果您尝试根据下拉列表中的值滚动到页面上的元素,则必须提供一个示例,让我们更接近您想要的内容,以便我们可以解决该特定方案。

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

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