简体   繁体   English

模态ui对话框中的jquery UI自动完成 - 建议未显示?

[英]jquery UI autocomplete inside a modal ui dialog - suggestions not showing?

i am using the jquery ui autocomplete widget inside the jquery ui dialog.我在 jquery ui 对话框中使用 jquery ui 自动完成小部件。 when i type in the search text, the textbox indents (ui-autocomplet-loading) but does not show any suggestions.当我输入搜索文本时,文本框缩进(ui-autocomplet-loading)但不显示任何建议。

var availableTags = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"];

$("#company").autocomplete({        
    source : availableTags ,
minLength: 2
});

company is the id of the textbox to attach the autocomplete. company 是附加自动完成的文本框的 ID。

i thought it might be az index so i set this:我认为它可能是 az index 所以我设置了这个:

.ui-autocomplete, .ui-menu, .ui-menu-item {  z-index: 1006; }

but it still does not show.但它仍然没有显示。 i put the autocomplete in a 'regular' page and it works fine.我将自动完成功能放在“常规”页面中,效果很好。

i am using jquery ui version 1.8.2.我正在使用 jquery ui 版本 1.8.2。 any ideas of where to look?关于在哪里看的任何想法?

I came across this answer when searching for this same issue, however none of the solutions were exactly what I wanted.我在搜索同样的问题时遇到了这个答案,但是没有一个解决方案正是我想要的。

Using appendTo worked, sorta... The autocomplete items showed up where they were supposed to, however it completely threw my dialog window into a garbled mess of improperly repositioned div elements.使用appendTo工作,排序......自动完成项目出现在它们应该出现的位置,但是它完全将我的对话框窗口扔进了乱七八糟的乱码,重新定位不正确的 div 元素。

So in my own css file, I created the following:所以在我自己的 css 文件中,我创建了以下内容:

ul.ui-autocomplete {
    z-index: 1100;
}

I'm sure 1100 is a little overkill, but I just wanted to play it safe.我敢肯定 1100 有点矫枉过正,但我​​只是想安全一点。 It works well and conforms with the KISS method.它运行良好并且符合 KISS 方法。

I'm using jQuery 1.9.2 with jQueryUI 1.10.2.我将 jQuery 1.9.2 与 jQueryUI 1.10.2 一起使用。

When using jQuery UI 1.10, you should not mess around with z-indexes ( http://jqueryui.com/upgrade-guide/1.10/#removed-stack-option ).使用 jQuery UI 1.10 时,您不应该乱用 z-indexes ( http://jqueryui.com/upgrade-guide/1.10/#re​​moved -stack-option )。 The appendTo option works, but will limit the display to the height of the dialog. appendTo选项有效,但会将显示限制在对话框的高度。

To fix it: make sure the autocomplete element is in the correct DOM order with: autocomplete .insertAfter( dialog .parent())修复它:确保自动完成元素处于正确的 DOM 顺序: autocomplete .insertAfter( dialog .parent())

Example例子

 var autoComplete,
     dlg = $("#copy_dialog"),
     input = $(".title", dlg);

 // initialize autocomplete
 input.autocomplete({
     ...
 });

 // get reference to autocomplete element
 autoComplete = input.autocomplete("widget");

 // init the dialog containing the input field
 dlg.dialog({
      ...
 });

 // move the autocomplete element after the dialog in the DOM
 autoComplete.insertAfter(dlg.parent());

Update for z-index problem after dialog click单击对话框后更新 z-index 问题

The z-index of the autocomplete seems to change after a click on the dialog (as reported by MatteoC).单击对话框后,自动完成的 z-index 似乎发生了变化(由 MatteoC 报告)。 The workaround below seems to fix this:下面的解决方法似乎可以解决这个问题:

See fiddle: https://jsfiddle.net/sv9L7cnr/见小提琴: https ://jsfiddle.net/sv9L7cnr/

// initialize autocomplete
input.autocomplete({
    source: ...,
    open: function () {
        autoComplete.zIndex(dlg.zIndex()+1);
    }
});

I solved it in bootbox like this:我在引导箱中解决了这个问题:

$( "#company" ).autocomplete({       
    source : availableTags ,
    appendTo: "#exportOrder"
});

You only have to append the result list to your form.您只需要将结果列表附加到您的表单中。

在您的autocomplete实现中,添加appendTo: "#search_modal" ,其中search_modal是您的模式的 ID。

for one or multiples autocompletes in the same dialogbox:对于同一对话框中的一个或多个自动完成:

// init the dialog containing the input field
 $("#dialog").dialog({
      ...
 });

// initialize autocomplete
 $('.autocomplete').autocomplete({        
      source: availableTags,
      minLength: 2
 }).each(function() {
      $(this).autocomplete("widget").insertAfter($("#dialog").parent());
 });
    $("#company").autocomplete({        
    source : availableTags ,
    appendTo : $('#divName')
    minLength: 2
});

Note: $('#divName') divName will be the name of modal body.注意:$('#divName') divName 将是模态体的名称。

EXAMPLE:例子:

<form id="exportOrder">
        <div class="input-group">
            <input type="text" id="accountReferenceSearch" placeholder="Type Account Reference to search..." class="form-control" style="width:500px">
        </div>
    </div>
</form>

self.AttachAutoComplete = function () {
                    $('#accountReferenceSearch').focus(function () {
                        $('#accountReferenceSearch').autocomplete({
                            source: function (request, response) {},
                            minLength: 2,
                            delay: 300,
                            appendTo: $("#exportOrder")
                        });
                    });
                }

I was experiencing the same problem, when it occurred to me:当我想到时,我遇到了同样的问题:

Always declare your dialog BEFORE setting autocomplete.始终在设置自动完成之前声明您的对话框。

I've switched them around, et voilà!我已经把它们换了,等等!

Autocomplete configure itself around the INPUT you target.自动完成围绕您的目标 INPUT 进行自我配置。 Dialog creates new markup and CSS around the container you target. Dialog 在您定位的容器周围创建新的标记和 CSS。 My choices where appearing behind the dialog, or off screen.我的选择出现在对话框后面或屏幕外。

I solved adding atribute z-index:1500 to my divs in jquery.autocomplete.css because Jquery UI Dialog put z-index between 1000 and 1005我解决了在 jquery.autocomplete.css 中将属性z-index:1500添加到我的 div 的问题,因为 Jquery UI Dialog 将 z-index 置于 1000 和 1005 之间

ul.auto-complete-list {
    list-style-type: none;
    margin: 0;
    padding: 0;
    position: absolute;
    z-index: 1500;
    max-height: 250px;
    overflow: auto;
}

I recently had the same issue.我最近有同样的问题。 Adding this to my css-file solved it for me:将此添加到我的 css 文件中为我解决了它:

.ui-autocomplete-input, .ui-menu, .ui-menu-item {  z-index: 2006; }

I first tried your initial z-index value of 1006 , but that didn't work.我首先尝试了您的初始 z-index 值1006 ,但这没有用。 Increasing the value worked for me.增加价值对我有用。

We had the same issue.我们有同样的问题。 We just updated our css so that z-index is high enough and cannot be overwritten:我们刚刚更新了我们的 css 以便 z-index 足够高并且不能被覆盖:

.dropdown-menu {
  z-index: 9999 !important;
}

As append to body makes the dropdown child of $window you need to make all dropdown-menu to the new z-index.由于追加到 body 使 $window 的下拉子菜单,您需要将所有下拉菜单添加到新的 z-index。

I had this problem also.我也有这个问题。 I am working in UserFrosting which has bootstrap and select2, but doesn't have jquery-ui in the core.我在具有 bootstrap 和 select2 的 UserFrosting 工作,但核心中没有 jquery-ui。 My autocomplete was inside a modal popup where the script tag and $(document) .ready are after the html for the modal form.我的自动完成功能位于模态弹出窗口中,其中脚本标记和$(document) .ready 位于模态表单的 html 之后。 After chasing all kinds of nonexistent conflicts and trying to make select2 (v 4) into a combobox, I went back to the css hack and this worked:在追逐各种不存在的冲突并尝试将 select2 (v 4) 变成一个组合框之后,我回到了 css hack,这很有效:

.ui-autocomplete {z-index: 1061 !important;}

My environment is我的环境是

  • UserFrosting with jquery 1-11.2使用 jquery 1-11.2 的 UserFrosting
  • bootstrap-3.3.2,引导程序-3.3.2,
  • bootstrap-modal引导模式
  • select2 v3.5.1选择2 v3.5.1
  • jquery-ui-1.11.4 (dot-luv theme) jquery-ui-1.11.4 (dot-luv 主题)

在我的情况下,在 css 中添加样式不起作用,但是在将zIndex属性完全添加到 jQuery ui 元素构造函数之后,它就像一个魅力。

Add the following method to your javascript and call it from the onload event of the body element:将以下方法添加到您的 javascript 并从 body 元素的 onload 事件中调用它:

//initialization
function init(){
    var autoSuggestion = document.getElementsByClassName('ui-autocomplete');
    if(autoSuggestion.length > 0){
        autoSuggestion[0].style.zIndex = 1051;
    }
}

It works for me :) I got this by looking at the computed style of the modal dialog to see what it's z-index was.它对我有用 :) 我通过查看模态对话框的计算样式来了解它的 z-index 是什么。 So all the function does is grab the auto suggestion box that's created by jquery (by one of it's class names which may be different for you but the idea is still the same) and modify it's css to include a z-index 1 point higher than the z-index of the modal (which was 1050)所以所有的功能就是抓取由jquery创建的自动建议框(通过它的一个类名,这对你来说可能不同但想法仍然相同)并修改它的css以包含一个z-index 1点高于模态的 z-index(为 1050)

This did the trick for me:这对我有用:

ul.ui-front {
    z-index: 1100;
}

Try This:-尝试这个:-
my_modal : modal id my_modal : 模态ID
Use appendTo property of autocomplete使用autocompleteappendTo属性

$("#input_box_id").autocomplete({<br> source:sourceArray/link,<br> appendTo :"#<i>my_modal</i>"<br> }); reference: https://stackoverflow.com/a/22343584/5803974参考: https ://stackoverflow.com/a/22343584/5803974

Johann-S ' answer here worked for me. Johann-S在这里的回答对我有用。

simply add data-focus="false" to the button or link calling the modal like只需将 data-focus="false" 添加到按钮或链接调用模态,如

<button type="button" class="btn btn-primary" 
 data-toggle="modal" 
 data-focus="false"
data-target=".bd-example-modal-sm">Small modal</button>

This way you don't have to mess with z-indexes or override bootstrap's enforce focus (it's not called)这样您就不必弄乱 z-indexes 或覆盖引导程序的强制焦点(它没有被调用)

I got the same issue, but after some struggle, I use Firefox to find solution.我遇到了同样的问题,但经过一番努力,我使用 Firefox 寻找解决方案。

In chrome when any autocomplete view is opened and you want to inspect the element, that will be disappear, when you click on any component.在 chrome 中,当打开任何自动完成视图并且您想要检查元素时,当您单击任何组件时,该元素将消失。

but

In Firefox, that will be displayed continuously so we can find the view at all.在 Firefox 中,它将连续显示,因此我们可以找到视图。

and I did the same thing with that, and I found a class which is having z-index我做了同样的事情,我发现了一个有 z-index 的类

z-index: 1000

so I just changed this to more like所以我只是把它改成了更像

.pac-container{
    z-index: 999999;
}

might be this z-index solution will not work for you all, but I am sure the feature of Firefox will definitely help you to find the best solution.可能这个 z-index 解决方案并不适合你们所有人,但我相信 Firefox 的功能肯定会帮助您找到最佳解决方案。

I was facing same issue , It has been resolved by adding bellow styles :我遇到了同样的问题,通过添加波纹管样式已解决:

.ui-autocomplete { position: absolute;  cursor: default;z-index:30!important;}  
.modal-dialog {pointer-events:auto !important;}

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

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