简体   繁体   English

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

[英]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.

相关问题 操作方法:修改jquery.autocomplete正则表达式以显示以给定输入开头的结果 - How To: Modify jquery.autocomplete regex to show results that starts with the given input 如何在jquery.autocomplete函数中获取对调用控件的引用 - How to get reference to calling control in jquery.autocomplete function 调试器未达到 jQuery.autocomplete 中的 select 语句 - Debugger not reaching select statement in jQuery.autocomplete 绕过jQuery.autocomplete上的所有缓存(1.02) - Bypass all cacheing on jQuery.autocomplete(1.02) 如何使用 jQuery.get &amp; jQuery.Autocomplete 导入 JSON 数据 - How to import JSON data using jQuery.get & jQuery.Autocomplete jQuery-如何将文本值写入隐藏字段以供以后使用? - jQuery - How to write text values to a hidden field for later use? jquery.autocomplete在选项卡上恢复为无效的先前建议 - jquery.autocomplete reverts to invalid previous suggestion on tab 如何使用隐藏字段选择具有ID /值对的jQuery自动完成功能 - How to select jquery autocomplete with ID/value pairs using a hidden field jQuery就地编辑器使用jQuery自动完成输入字段 - jQuery in-place-editor to use jQuery autocomplete input field 在鼠标按下/输入时重置文本字段 - jQuery UI 自动完成 - Reset text field on Mouse press / Input - jQuery UI Autocomplete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM