$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$("#tags").autocomplete({
source: availableTags
});
var count = 1;
var cloneId = 'clone-';
$('#tryClone').on('click', function() {
var tag2 = $('#tags').clone();
tag2.removeAttr("id");
count += 1;
var elementId = cloneId + count;
$(tag2).attr("id", elementId).val(elementId);
$(tag2).autocomplete({
source: availableTags
}).autocomplete('enable');
$(tag2).appendTo('#container');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
<div id="container">
</div>
<button id="tryClone">
Try clone and bind
</button>
</div>
这个新元素是由动态创建的? –
是的,这是由.clone()方法创建的 –
然后我认为你应该使用委托()为此工作正常 –