我正在使用typeahead.js
我有一个带有两个输入的一行表,(代码和产品),后者使用typeahead输入,一旦我选择了typeahead选项,它也会填充代码输入 .
这两个输入最初加载在页面上 . 然后通过单击按钮添加具有相同两个输入(不同名称)的新行,根据用户请求添加其他按钮 .
我有一个JS var(number = 0)来控制添加的动态输入,在单击添加行按钮时递增,每次单击添加行按钮时我都会调用typeahead函数 .
HTML代码:
<td>
<input type="text" name="code_product_0" id="code_product_0">
</td>
<td>
<input type="text" name="product_0" id="product_0">
</td>
<a class="btn" href='javascript:addRow(); typeahead();' id="btn-new-product"> Add Product</a>
这是addRow代码:
function addRow() {
number++;
$('#table').append(
"<tr>" +
"<td><input name='code_product_"+number+"' id='code_product_"+number+"' type='text' class='form-control'></td>" +
"<td><input class='typeahead form-control' type='text' name='product_"+number+"' id='product_"+number+"'></td>" +
"</tr>");
}
这是预先输入的代码:
$('#product_'+number).typeahead(null, {
name: '#product_'+number,
displayKey: 'name',
source: custom.ttAdapter()
}).on('typeahead:selected', function(event, data){
$('#product_'+number).val(data.name);
$('#code_product_'+number).val(data.code);
});
这里的问题是,当我有2行或更多行时,无论我想要更改哪个产品输入(除了最后一行),产品输入都会正确更改,但它会填充最后一行的代码输入而不是我正在更改的那一行