jQueryUI autocomplete not working with dialog and zIndex

前端 未结 14 1124
孤独总比滥情好
孤独总比滥情好 2021-02-05 00:51

I ran into an interesting issue with jQueryUI autocomplete in a dialog box.

My dialog HTML looks like this:

14条回答
  •  佛祖请我去吃肉
    2021-02-05 01:09

    When using jQuery UI 1.10, you should not mess around with z-indexes (http://jqueryui.com/upgrade-guide/1.10/#removed-stack-option). The appendTo option works, but will limit the display to the height of the dialog.

    To fix it: make sure the autocomplete element is in the correct DOM order with: 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

    The z-index of the autocomplete seems to change after a click on the dialog (as reported by MatteoC). The workaround below seems to fix this:

    See fiddle: https://jsfiddle.net/sv9L7cnr/

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

提交回复
热议问题