[英]jQuery clone with ajax autocomplete
The achieved idea is an invoice, with fields item, description, rate, etc. Each line of fields, with a add row - achieved by jquery clone. 实现的想法是一张发票,其中包含字段项目,说明,费率等。每行字段都有一个添加行-由jquery clone实现。 And it is working. 它正在工作。
However, I also have a working autocomplete, which gets the data from a database, and update the rest of the fields. 但是,我也有一个有效的自动完成功能,该功能可以从数据库中获取数据,并更新其余字段。
But I cant get the two to work together. 但是我不能让两者一起工作。 I don't know how else to explain it. 我不知道该怎么解释。
So; 所以;
<script type="text/javascript">
$(function() {
$( "#label" ).autocomplete({
source: "<?=base_url()?>admin/autocomplete/",
minLength: 1,
select: function( event, ui ) {
event.preventDefault();
this.value = ui.item.valuedesc;
$('#label').val(ui.item.valuedesc);
$('#description').val(ui.item.description);
$('#rate').val(ui.item.rate);
}
});
});
</script>
Then the return I get is the valuedesc, description, rate, which then automatically updates the relevan fields. 然后,我得到的回报是valuedesc,描述,费率,然后它会自动更新relevan字段。
<input type="text" maxlength="255" name="item_sku[]" data-required="1" class="form-control" id="label" autocomplete="off" required/>
<input type="text" maxlength="255" name="item_description[]" data-required="1" id="description" class="form-control" required/>
<input type="text" maxlength="255" name="item_amount[]" data-required="1" id="rate" class="form-control calculate" autocomplete="off" required/>
And this is what I use to clone the row and clears the input values of the newly added row 这就是我用来克隆行并清除新添加行的输入值的方法
// add new product row on invoice
var cloned = $('#invoice_table tr:last').clone();
$(".add-row").click(function (e) {
e.preventDefault();
cloned.clone().appendTo('#invoice_table').find('input').val('');
});
So, the autocomplete works for the first row - because I get the "label" id from the text input. 因此,自动完成功能适用于第一行-因为我从文本输入中获得了“标签” ID。
But if I add more, getting the autocomplete to work doesn't work, because of duplicate IDs. 但是,如果我添加了更多内容,则由于重复的ID,自动填充功能无法正常工作。
My question would be thus; 我的问题就是这样; How can I dynamically add a unique ID to where "label" id currently is? 如何在当前“标签” ID所在的位置动态添加唯一ID? Secondly, is it possible to alter the autocomplete javascript to prevent duplicating the javascript for each clone? 其次,是否可以更改自动完成的javascript以防止为每个克隆复制javascript?
Image here: http://i.stack.imgur.com/RxFCN.jpg 图片在这里: http : //i.stack.imgur.com/RxFCN.jpg
The simplest way of doing it is to use classes instead of id-s: 最简单的方法是使用类而不是id-s:
<input type="text" maxlength="255" name="item_sku[]" data-required="1" class="form-control label" autocomplete="off" required/>
<!-- ... etc ... -->
and then initialise a new autocomplete every time you generate a new row: 然后在每次生成新行时初始化一个新的自动完成功能:
$(function() {
var options = {
source: "<?=base_url()?>admin/autocomplete/",
minLength: 1,
select: function( event, ui ) {
event.preventDefault();
this.value = ui.item.valuedesc;
$(this).val(ui.item.valuedesc);
$(this).parents('tr').find('.description').val(ui.item.description);
$(this).parents('tr').find('.rate').val(ui.item.rate);
}
};
$( ".label" ).autocomplete(options);
var cloned = $('#invoice_table tr:last').clone();
$(".add-row").click(function (e) {
e.preventDefault();
cloned.clone().appendTo('#invoice_table').find('input').val('')
.filter('.label').autocomplete(options);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.