我有这个添加两个字段的代码,我想让输入具有自动完成功能:
$("#addElem").click(function () {
var new_id = new Date().getTime();
var content = '<div id="fields"><input type="hidden" name="softwarePerAsset.Index" value="' + new_id + '" />' +
'<select name="softwarePerAsset[' + new_id +
'].name" id="softwarePerAsset[' + new_id +
'].name"> <option value="1">Visio</option><option value="2">Painter</option></select> ' +
'<input type="text" name="softwarePerAsset[' + new_id + '].serial" id="softwarePerAsset[' + new_id +
'].serial"> <a id="test" href="#">Remove Software</a></div>';
$("#elem").append(content);
});
我试过添加这个,但没有任何效果:
var data = "notepad firefox chrome".split(" ");
content.find('input[type=text]').autocomplete(data);
我错过了什么?
更新 #1 - 完整页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Index
</title></head>
<body>
<form action="/element/edit" method="post">
<div id="elem">
<!-- If there are software installed, then s -->
<div class="fields">
<input type="hidden" name="softwarePerAsset.Index" value="1" />
<select name="softwarePerAsset[1].name" id="softwarePerAsset[1].name">
<option value="1">Visio</option>
<option value="2">Painter</option>
</select>
<input type="text" name="softwarePerAsset[1].serial" id="softwarePerAsset[1].serial" />
<a class="test" href="#">Remove Software</a>
</div>
<div class="fields">
<input type="hidden" name="softwarePerAsset.Index" value="2" />
<select name="softwarePerAsset[2].name" id="softwarePerAsset[2].name">
<option value="1">Visio</option>
<option value="2">Painter</option>
</select>
<input type="text" name="softwarePerAsset[2].serial" id="softwarePerAsset[2].serial" />
<a class="test" href="#">Remove Software</a>
</div>
</div><a href="#" id="addElem">Add Element</a>
<br />
<br />
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<script src="/jquery-1.9.1.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
//add element to elem div
$("#addElem").click(function () {
var new_id = new Date().getTime();
var content = '<div class="fields"><input type="hidden" name="softwarePerAsset.Index" value="' + new_id + '" />' +
'<select name="softwarePerAsset[' + new_id +
'].name" id="softwarePerAsset[' + new_id +
'].name"> <option value="1">Visio</option><option value="2">Painter</option></select> ' +
'<input type="text" name="softwarePerAsset[' + new_id + '].serial" id="softwarePerAsset[' + new_id +
'].serial"> <a id="test" href="#">Remove Software</a></div>';
$("#elem").append(content);
var data = "notepad firefox chrome".split(" ");
$(content).find('input[type=text]').autocomplete(data);
});
//remove fields
$("#elem").on("click", ".test", function () {
$(this).closest("div.fields").remove();
});
});
</script>
</body>
</html>