JqueryUi Autocomplete shown in wrong place when us

2019-04-19 07:29发布

I have a "modal window" in a webpage obtained by applying to a div the css property position to fixed. The div contains input fields. In particular, I'm using the autocomplete widget from jQueryUI. There are two major problems:

1) the first is that, since the div has a fixed position, when you scroll down the webpage, the autocomplete suggestions are not shown fixed but are moved up and down with the page. You can see that problem at this Codepen where I'm using an example from jQuery website with city-name autocompletion.

2) The second problem is that the suggestions from autocomplete are shown in the upper-left corner of the page and not just below the input field. Unfortunately, I tried to reproduce this problem in Codepen, but I can't.

I'm quite sure that the problem is due to the CSS, and in particular to such style properties provided by the JQuery-UI. Maybe the problem can be resolved by using the position option of the autocomplete widget.

What CSS property should I define and how?

PS: I use the theme provided at http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css.

3条回答
Summer. ? 凉城
2楼-- · 2019-04-19 07:34

Make sure you only load one version of Jquery and one of the UI I discovered this after reading @Riapp's answer, I played around and could see that you need matching version numbers and never too many....

Otherwise the autocomplete will be an absolute, and fly to the top.

 <script type="text/javascript"
    src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css"
    href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" />
查看更多
Fickle 薄情
3楼-- · 2019-04-19 07:34

had the same problem. Delete jquerUI base themes, Uninstall jqueryUI library then reinstall jqueryUI and now works fine. Not sure what you're working with but I'm on MVC4 so many bugs, I'm about to kill myself. Good Luck.

EDIT: this was the real problem

In looking at an older project to see if the autocomplete was working we found that many of the styles in the older project were not in the newer. Not sure if I erased them but can't imagine doing so. All I had to do was copy and paste those classes that were missing from my new project and everything went back to normal. I think someone is sabotaging my project.

查看更多
劳资没心,怎么记你
4楼-- · 2019-04-19 07:47

Looking at the jQuery UI library, I see that the <ul> element is used to show the suggestions. jQuery UI appends this element by default to the HTML document and not to the <div> (used as "modal window") of the text input.

However, the documentation shows that the option appendTo configures which element the autocomplete <ul> should be appended to. It uses jQuery's appendTo() function. http://jqueryui.com/demos/autocomplete/#option-appendTo

So, I included a div to contains the suggestions:

<input type="text" id="myInput" />
<div id="container">
</div>

And in the autocomplete() function I added the option:

appendTo: "#container"

Then I added the following CSS

#container {
    display: block; 
    position:relative
} 
.ui-autocomplete {
    position: absolute;
}

That's all!

查看更多
登录 后发表回答