如何在隐藏的输入字段(type =“text”)上使用jQuery.autocomplete?

时间:2011-10-05 08:48:46

标签: javascript jquery jquery-autocomplete

我正在尝试在隐藏div中的文本字段上使用jQuery的自动完成功能。虽然输入字段获得了ui-autocomplete-input类,但它似乎不起作用。这就像没有事件受我的输入约束。

为了清楚起见,代码如下所示:

<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>

在此之后,#my-unique-id获得ui-autocomplete-input类但没有绑定事件。 有什么想法吗?

这是服务器端脚本,但它没有任何问题,因为在另一个字段上它运行正常:

(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);
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码是正确的,可能出错的是您的服务器端脚本没有返回数据,因此看起来没有任何事情发生。你能发贴服务器端脚本吗?

您也可以尝试使用本地数据源:

    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
    });