我正在点击按钮创建输入,我想为其添加自动完成功能
请注意,它适用于静态元素
我的HTML代码
<div class="pharmaSec">
<div class="form-group" id="medicineTable">
<div class="col-sm-8 selectContainer">
<input name="OTCtext[]" id="OTCtexts" class="form-control medicinefind"/>
</div>
<div class="col-sm-4 selectContainer">
<input type="text" class="form-control" placeholder="Qts" value="1"/>
</div>
</div>
<div class="form-group" id="medicineTableTemp">
<div class="col-sm-8 selectContainer">
<input name="OTCtext[]" id="OTCtext" class="form-control medicinefind">
<input type="hidden" id="hiddenValue" value="0">
</div>
<div class="col-sm-4 selectContainer">
<input type="text" class="form-control" placeholder="Qts" value="1"/>
</div>
</div>
我的js代码
$(document).ready(function () {
$('#btn_add_new').click(function () {
$('#medicineTable').append($('#medicineTableTemp').html());
$("input.medicinefind:last").focus();
});
});
$("input.medicinefind").live("keydown.autocomplete", function() {
$(this).autocomplete({source: '/medicineSearch', minLength: 1});
});
感谢所需的所有帮助。
答案 0 :(得分:1)
我找到了答案以防万一,如果任何一个人遇到同样的问题
var counter = 0;
$('#btn_add_new').click(function () {
counter = counter+1;
var id = "OTCtext"+counter;
var template = ' <div class="col-sm-8 selectContainer"><input name="OTCtext[]" id="'+id+'" class="form-control medicinefind"><input type="hidden" id="hiddenValue" value="0"></div><div class="col-sm-4 selectContainer"><input type="text" name="medicine_qts[]" class="form-control" placeholder="Qts" value="1"/></div>';
$('#medicineTable').append(template);
$('#OTCtext' + counter).autocomplete({source: '/medicineSearch', minLength: 1});
});