gpt4 book ai didi

javascript - 动态元素上的自动完成

转载 作者:行者123 更新时间:2023-12-01 03:58:24 25 4
gpt4 key购买 nike

我每次都在这个问题上挣扎,今天又如此。我正在附加新的文本元素,并且我想将自动完成功能绑定(bind)到它。

此代码与之前的 onClick 一起使用

$('#highlights').before("<input type='text' id='check_name' name='name' placeholder='Name of OPM' required class='ui-autocomplete-input'>");

此自动完成功能位于网页末尾

<script type="text/javascript">
$("#check_name").autocomplete({
source: "src/check_name.php",
minLength: 1
});
</script>

当我通过 URL 访问 PHP 文件时,它运行良好。

最佳答案

Because .before(): insert content, specified by the parameter, before each element in the set of matched elements.

您可以使用.prev() :

代码片段:

var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];


$('#highlights').before("<input type='text' id='check_name' name='name' placeholder='Name of OPM' required class='ui-autocomplete-input'>")
.prev("#check_name") // get the newly prev added ele
.autocomplete({
source: availableTags,
minLength: 1
});
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>


<div id="highlights"></div>

关于javascript - 动态元素上的自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42420185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com