admin管理员组文章数量:1405551
I have a page where I want to dynamically add more text fields and for this purpose I'm using the jQuery .append function. However, I also want to have those text fields being autopleted by using yet another jQuery function. Here's what I have as JavaScript:
<script>
$( document ).ready(function() {
$("#addInputs").click(function() {
$("#inputList").append("<input type='text' id='autoplete' size='35' name='sales[]' /><br />");
});
});
</script>
<script>
$(function ac() {
var availableTags = [
"ActionScript",
"AppleScript",
"Scheme"
];
$( "#autoplete" ).autoplete({
source: availableTags
});
});
</script>
I have the following HTML:
<div id='inputList'>
<input type='text' id='autoplete' size='35' name='sales[]' /><br />
</div>
<a id='addInputs' />Add Inputs</a>";
The autoplete works for the text field that I already have displayed on the screen once the page loads. The "Add Inputs" also works and correctly appends more fields on the screeen.
The problem is that those newly added fields do not trigger the autoplete function. Any ideas how this could be achieved?
Thank you!
I have a page where I want to dynamically add more text fields and for this purpose I'm using the jQuery .append function. However, I also want to have those text fields being autopleted by using yet another jQuery function. Here's what I have as JavaScript:
<script>
$( document ).ready(function() {
$("#addInputs").click(function() {
$("#inputList").append("<input type='text' id='autoplete' size='35' name='sales[]' /><br />");
});
});
</script>
<script>
$(function ac() {
var availableTags = [
"ActionScript",
"AppleScript",
"Scheme"
];
$( "#autoplete" ).autoplete({
source: availableTags
});
});
</script>
I have the following HTML:
<div id='inputList'>
<input type='text' id='autoplete' size='35' name='sales[]' /><br />
</div>
<a id='addInputs' />Add Inputs</a>";
The autoplete works for the text field that I already have displayed on the screen once the page loads. The "Add Inputs" also works and correctly appends more fields on the screeen.
The problem is that those newly added fields do not trigger the autoplete function. Any ideas how this could be achieved?
Thank you!
Share Improve this question asked Jun 19, 2013 at 9:34 mmvsbgmmvsbg 3,58817 gold badges54 silver badges74 bronze badges5 Answers
Reset to default 4The issue is that the code uses the same id for every appended element. Ids must be unique. Refactor the code so that it sets a class attribute on the elements appended to the DOM. Then use the class as the selector when you call the autoplete
function.
Javascript
<script>
var availableTags = [
"ActionScript",
"AppleScript",
"Scheme"
];
$( document ).ready(function() {
$("#addInputs").click(function() {
//Adding the class
$("#inputList").append("<input type='text' class='autoplete' size='35' name='sales[]' /><br />");
//Selector using the class
$( ".autoplete" ).autoplete({
source: availableTags
});
});
});
</script>
try this :
$(document).on("focus", "#autoplete", function(e) {
if ( !$(this).data("autoplete") ) {
$(this).autoplete({
source: availableTags
});
}
});
on my site works. ;)
Once you add a new input element you need to initialize the widget on them.
$(function () {
function autoplete(el){
$( el ).autoplete({
source: availableTags
});
}
var availableTags = [
"ActionScript",
"AppleScript",
"Scheme"
];
autoplete($("#autoplete"))
$("#addInputs").click(function() {
var el = $("<input type='text' size='35' name='sales[]' /><br />").appendTo("#inputList");
autoplete(el)
});
});
In HTML ID attribute should contain unique value, but not CLASS attribute so instead of id use class (class = "autoplete")
I think that the problem occurred because of the .autoplete
function is binded just once for the elements already in the DOM, my suggestion is using the .live
or .on
binding so later elements can be binded to:
$('#autoplete').on("focus", function(e) {
if ( !$(this).data("autoplete") ) {
$(this).autoplete({
source: availableTags
});
}
});
本文标签: javascriptjQueryautocompleteappend functionsStack Overflow
版权声明:本文标题:javascript - jQuery - autocomplete + append functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744304035a2599723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论