[英]How to use jQuery.autocomplete on a hidden input field (type=“text”)?
I am trying to use the autocomplete from jQuery on an text field inside a hidden div. 我试图在隐藏的div内的文本字段上使用jQuery的自动完成功能。 It doesn't seem to work, though the input field gains the ui-autocomplete-input class.
尽管输入字段获得了ui-autocomplete-input类,但它似乎不起作用。 It's like no event is bound to my input.
就像没有事件绑定到我的输入上一样。
Just to be clear, the code looks like this: 为了清楚起见,代码如下所示:
<a href="#overlay" class="open-the-overlay-that-has-the-id-like-this-href-attribute">OPEN</a>
<div id="overlay" style="display:none">
<form action="/action" method="post">
<input type="text" id="my-unique-id" />
<input type="submit" />
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#my-unique-id").autocomplete({
source: "search.php",
minLength: 2
});
});
</script>
After this, the #my-unique-id gains the ui-autocomplete-input class but has no events bound. 此后,#my-unique-id获得ui-autocomplete-input类,但没有绑定任何事件。 Any ideas?
有任何想法吗?
Here is the server side script, but it's nothing wrong with it, because on another field it works fine: 这是服务器端脚本,但是没有问题,因为在另一个字段上它可以正常工作:
(CakePHP) (CakePHP)
function getCity() {
$this->layout = FALSE;
$this->autoRender = FALSE;
Configure::write("debug",0);
if(isset($_GET["term"]) && !empty($_GET["term"])) {
AppModel::unbind($this->City);
preg_match_all("/(\w+)/",urldecode($_GET["term"]), $term);
$or = array();
foreach ($term[0] as $t) {
if(strtolower($t) != "jud")
$or[] = "City.name LIKE '%$t%'"; // OR City.county LIKE '%$t%'
}
$or = implode(" OR ", $or);
$cities = $this->City->find("all", array("limit"=>30, "fields"=>array("City.id", "City.name", "City.county"),"conditions"=>array($or)));
$returnArray = array();
$k = 0;
foreach($cities as $c) {
$returnArray[$k]['id'] = $c["City"]["id"];
$returnArray[$k]['label'] = $c["City"]["name"].", jud. ".$c["City"]["county"];
$returnArray[$k]['value'] = $c["City"]["name"].", jud. ".$c["City"]["county"];
$k++;
}
return json_encode($returnArray);
}
}
Your code is correct, what could be wrong is your server side script which is not returning data and so it looks like nothing is happening. 您的代码是正确的,可能出问题的是您的服务器端脚本未返回数据,因此看起来什么也没有发生。 Could you please post your server side script?
您能否发布服务器端脚本?
You could also try with a local datasource: 您也可以尝试使用本地数据源:
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$("#my-unique-id").autocomplete({
source: availableTags ,
minLength: 2
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.