简体   繁体   English

jQuery自动完成功能不起作用…

[英]jQuery autocomplete doesn't work…

I am using the autocomplete functionality from jQuery and am having a small problem. 我正在使用jQuery的自动完成功能,并且遇到了一个小问题。 I have a div element with some text and a variable number of input boxes that have autocomplete functionality. 我有一个div元素,其中包含一些文本和数量可变的具有自动完成功能的输入框。 When a user clicks on the text in the div element, the input boxes are removed. 当用户单击div元素中的文本时,输入框将被删除。 Clicking on the text toggles between showing the input boxes and not showing the input boxes. 单击文本可在显示输入框和不显示输入框之间切换。 With each text click a php script is called that returns new html text for the div element. 每单击一次文本,就会调用一个php脚本,该脚本返回div元素的新html文本。

The autocomplete inputs are created with php code similar to the following: 自动完成输入是使用类似于以下内容的php代码创建的:

php code PHP代码

echo "<id='a' value='Smith' autocomplete='off'>";
echo "<script type=\"text/javascript\">$('a').autocomplete('do_name_lookup.php',{minChars:3,max:999});</script>";
echo "<script type=\"text/javascript\">$('a').result(function(event, data, formatted) {alert(data[0])});</script>";

On a static web page the two script elements would appear in the " $(document).ready " function. 在静态网页上,两个script元素将出现在“ $(document).ready ”函数中。 Since my page is dynamic, however, the input boxes are created a long time after $(document).ready has fired. 但是,由于我的页面是动态的,因此在$(document).ready触发后很长时间就会创建输入框。 Therefore, I have to execute the javascript code just after each of the input boxes is made. 因此,我必须在制作完每个输入框之后执行javascript代码。 This setup works fine the first time the php code above is executed, but the second time the code is executed, the autocomplete functionality goes away. 第一次执行上述php代码时,此设置工作正常,但是第二次执行该代码时,自动完成功能消失了。

Can anyone explain to me why this is happening and how to work around it? 谁能向我解释为什么会这样以及如何解决? My suspicion is that I am declaring a variable with the same name multiple times and this is creating a conflict of some type. 我的怀疑是我多次声明了一个具有相同名称的变量,这造成了某种类型的冲突。

<id='a' value='Smith' autocomplete='off'>

This is no valid element, it misses a tag name. 这不是有效的元素,它缺少标签名称。 Want to amend " input "? 要修改“ input ”吗?

$('a')…

This selects all anchor elements (links) in the page. 这将选择页面中的所有锚元素(链接)。 I guess you wanted the id selector $('#a') . 我猜你想要id选择器$('#a')

Also, you should really combine the two script elements to one. 另外,您应该真正将两个脚本元素组合为一个。 Not only because there are limits in some browsers. 不仅是因为某些浏览器有限制。 A <script> may contain more than one JavaScript statement, and you just may want to chain the two methods of the same selected element. <script>可能包含多个JavaScript语句,您可能只想链接同一选定元素的两个方法

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

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