我在jsp中有一个文本框,如下所示
<input id="newHardware_details_0__hardware_type" name="details[0].hardware_type" class="autocompleter">
我正在使用jquery插件'autocomplete',如下所示:
$(function() {
var availableTypes = [ "Desktop",
"Laptop",
"Mobile",
"Tablet",
"Printer",
"Scanner",
"Server",
"Switch",
"Wifi-router",
"USB Dongle"
];
$( ".autocompleter" ).autocomplete({
source: availableTypes
});
});
我还有另一个添加按钮,我想添加带有'autocomplete'功能的simillar文本框。我能够从javascript函数动态创建文本框,但无法将此jquery插件自动完成程序附加到它。请注意,可以多次单击添加按钮,新生成的文本框将具有以下名称
细节[1] .hardware_type
细节[2] .hardware_type
详情[3] .hardware_type等..
Javascript代码如下:
function addRow(tableID) {
//alert ("adding row");
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var idx = rowCount+1;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var hardware_typeElement = document.createElement("input");
hardware_typeElement.type = "text";
hardware_typeElement.name = "details[" + idx + "].hardware_type";
hardware_typeElement.id = "newHardware_details_" + idx + "__hardware_type";
hardware_typeElement.className = "autocompleter";
cell1.appendChild(hardware_typeElement);
var label = document.createElement("label");
var t = document.createTextNode("Hardware Type");
label.setAttribute("for", "newHardware_details_" + idx + "__hardware_type");
label.appendChild(t);
cell1.insertBefore(label,document.getElementById("newHardware_details_" + idx + "__hardware_type"));
}
我对Jquery很陌生。任何帮助将受到高度赞赏
答案 0 :(得分:1)
如下所示:
$(/*select your td here*/).append('input type="text" class="my-textbox"');
$(/*select same td*/).find('input.my-textbox').callPluginMethod();
更新:修改后的代码:
$("#trigger").click(function(){
addRow();
})
function addRow() {
$(tableID).append('<tr><td><input type="text" class="my-textbox"></input></td></tr>')
$(tableID).find('tr:last > input.my-textbox').callPluginMethod();
}