【问题标题】:Is there a way to filter out search results from a chosen jquery select?有没有办法从选定的 jquery 选择中过滤掉搜索结果?
【发布时间】:2014-06-05 11:34:14
【问题描述】:

我从所选下拉菜单中隐藏了一个特定选项。我无法在法庭上删除它,因为我需要 IE6 上的该选项(!)。

我遇到的问题是,当我开始在输入字段中输入时,隐藏选项会重新出现在所选搜索结果中。

我也尝试使用 {display_disabled_options: false} 选项,但它不起作用(可能是因为我在选择已经初始化后禁用了该选项。

我正在尝试这样做:

    $(".chosen-select").chosen({
      display_disabled_options: false
    }).each(function () {
      $(this).on('chosen:showing_dropdown', function (event, params) {
        $('li:contains("whatever")').attr('disabled', true);
      });
    }); 

【问题讨论】:

    标签: javascript jquery jquery-chosen


    【解决方案1】:

    如果您需要在选择初始化后更新列表,您需要使用trigger("chosen:updated") 触发其更新。

    所以使用代码:

    $(function () {
        $('.chzn-select').chosen({
            display_disabled_options: false
        }).each(function () {
            $(this).on('chosen:showing_dropdown', function (event, params) {
                $('option:contains("shop")',$(this)).attr('disabled', true);
                $(this).trigger("chosen:updated");
            });
        });;
    });
    

    演示:http://jsfiddle.net/IrvinDominin/NfvBj/

    【讨论】:

    • 谢谢。它完美地工作。一个问题:为什么你在 jquery selection ($(this)) 调用中添加了第二个参数?
    • @eolo999 欢迎您!第二个参数用于为选择器提供上下文,在这种情况下,它将仅在当前选择的上下文中搜索选项元素,请参阅:api.jquery.com/jquery/#jQuery-selector-context
    猜你喜欢
    • 2018-11-02
    • 2019-05-29
    • 2017-04-02
    • 2015-07-10
    • 2022-12-17
    • 1970-01-01
    • 2013-04-02
    • 2022-06-19
    • 1970-01-01
    相关资源
    最近更新 更多