【问题标题】:jquery autocomplete - prevent closing it when clicking on outside div?jquery自动完成 - 单击外部div时防止关闭它?
【发布时间】:2013-09-09 08:08:02
【问题描述】:

所以 - jquery 自动完成和带有特殊字符图标的额外 div 可以插入到搜索框中(例如 É、ã 等)...

每当用户键入时 - 自动完成下拉菜单会显示,但当用户需要点击特殊字符图标(因此它会被插入到搜索框)时 -> 自动完成下拉菜单会在焦点丢失时自动关闭:(

当用户点击其中一个特殊字符图标时,有没有办法防止 jquery 自动完成下拉菜单在失去焦点时隐藏?

【问题讨论】:

    标签: javascript jquery jquery-ui


    【解决方案1】:

    编辑:这是针对与选择框一起使用的 jquery 自动完成组合框,但同样的解决方案和思考过程肯定也适用于自动完成(当我遇到你的时候,我正在处理组合框问题:)

    HTML:

    <select class="combobox">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="mercedes">Mercedes</option>
      <option value="add_another">Add Another Option</option>
    </select> 
    

    CSS:

    .force-open{
        display:block !important;
    }
    

    JS:

    $(function() {
    $( ".combobox" ).combobox({
    
         // the select event is apparently the only event
         // that you can pass a function, so if your goal
         // is to keep it open specifically when a user
         // clicks an option, then this is where you should start
    
         select: function(event, ui) {
                $(event.currentTarget)
                .parent()
                .next("ul")
                .addClass("force-open");
         }
    });
    
    
    $(".custom-combobox .ui-button").click(function(){
    
        //This is the actual answer to your question,
        //which is specifically forcing the jquery selectbox
        //to stay open after it has already been opened,
        //regardless of the change in focus.
    
        $(this)
        .parent()
        .next("ul")
        .addClass("force-open");
    })
    });
    

    说明:

    通常,如果在进行 Web 开发时(除了尝试破解/更新源代码......)对于给定的库或代码块没有好的暴露事件,我会尝试做的是查看结构从 library/js 生成的 html(如果有的话)。

    本例中,jquery-ui调用$(".combobox").combobox({...})后生成的html为:

    <select class="combobox" style="display: none;">
    ....shown above in HTML section...
    <span class="custom-combobox">
    <span class="ui-helper-hidden-accessible" role="status" aria-live="polite">4 results are available, use up and down arrow keys to navigate.</span>
    <input class="custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input" title="" autocomplete="off">
    <a class="ui-button ui-widget ui-state-default ui-button-icon-only custom-combobox-toggle ui-corner-right" tabindex="-1" title="Show All Items" role="button" aria-disabled="false">
    </span>
    <ul id="ui-id-1" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: none; width: 209px; top: 43px; left: 8px;">
    <li class="ui-menu-item" role="presentation">
    <a id="ui-id-2" class="ui-corner-all" tabindex="-1">Volvo</a>
    </li>
    <li class="ui-menu-item" role="presentation">
    <a id="ui-id-3" class="ui-corner-all" tabindex="-1">Saab</a>
    </li>
    <li class="ui-menu-item" role="presentation">
    <a id="ui-id-4" class="ui-corner-all" tabindex="-1">Mercedes</a>
    </li>
    <li class="ui-menu-item" role="presentation">
    <a id="ui-id-5" class="ui-corner-all" tabindex="-1">Add Another Option</a>
    </li>
    </ul>
    

    这为我提供了一个很好的起点,以便添加/删除 css 类以强制实现我想要的功能。请记住,这是最后的手段,只有在没有功能齐全的 api 来帮助绑定/取消绑定事件时才应被滥用。通过检查 html 结构,我可以使用 jquery 选择器/方法来获取我试图保持打开状态的正确“ul”标签,在这种情况下。因此,在我使用 jquery 获取我想要的 ul 之后,我添加了具有 'display:block !important;' 的类'force-open' ,最终迫使 jquery 组合框保持打开状态,无论焦点如何。现在,当您觉得是时候“关闭”组合框时,只需 removeClass 'force-open'。

    希望这会有所帮助:)。

    【讨论】:

      猜你喜欢
      • 2017-08-14
      • 2011-11-16
      • 2021-09-01
      • 2012-04-04
      • 2021-08-31
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多