简体   繁体   中英

Autocomplete on dynamically generated divs

I have problem with autocomplete on my dynamically generated divs

File 1 - Javascript file which on button click generate div with input fields

var inputHTML = '<div id="addProducts" class="col-md-2 form-group"><input type="text" class="form-control" placeholder="Identyfikator opony" name="tyreID[]" id="tyreID"><input type="text" class="tyreSize form-control" placeholder="Rozmiar" name="tyreSize[]" id="tyreSize" required><input type="text" class="tyreManufacturer form-control" placeholder="Producent" name="tyreManufacturer[]" id="tyreManufacturer" required><input type="text" class="tyreTread form-control" placeholder="Bieżnik" name="tyreTread[]" id="tyreTread" required><input type="text" class="form-control" placeholder="DOT" name="tyreDOT[]" id="tyreDOT"><input type="number" step="0.01" min="0" class="form-control price" placeholder="Cena za sztukę" name="tyrePricePiece[]" id="tyrePricePiece" required><button href="#" class="btn btn-danger btn-block remove_field"><span class="glyphicon glyphicon-remove"></span> Usuń</button><button type="button" class="btn btn-warning btn-block" id="clone"><span class="glyphicon glyphicon-duplicate"></span> Kopiuj</button></div>';
var inputHTML2 = '<div id="addProducts2" class="col-md-2 form-group"><input type="text" class="form-control" placeholder="Identyfikator felg" name="alloyID[]" id="alloyID"><input type="text" class="alloySize form-control" placeholder="Rozmiar" name="alloySize[]" id="alloySize" required><input type="text" class="alloyManufacturer form-control" placeholder="Producent" name="alloyManufacturer[]" id="alloyManufacturer" required><input type="text" class="alloyPCD form-control" placeholder="Rozstaw" name="alloyPCD[]" id="alloyPCD" required><input type="text" class="form-control" placeholder="Otwór centrujący" name="alloyHub[]" id="alloyHub"><input type="number" step="0.01" min="0" class="form-control price" placeholder="Cena za komplet" name="alloyPricePiece[]" id="alloyPricePiece" required><button href="#" class="btn btn-danger btn-block remove_field"><i class="fa fa-times"></i> Usuń</button><button type="button" class="btn btn-warning btn-block" id="clone"><span class="glyphicon glyphicon-duplicate"></span> Kopiuj</button></div>';

var addInput = function() {
    $(inputHTML).appendTo('div#products');
};

var addInput2 = function() {
    $(inputHTML2).appendTo('div#products');
};

var cloneInput = function() {
    $(this).appendTo('div#products');
}

$('button#btnAddProduct').click(addInput);
$('button#btnAddProduct2').click(addInput2);


$(document).on('click', '.remove_field', function(e) { //Once remove button is clicked
    e.preventDefault();
    $(this).parent('div').remove(0);
    sumPrice();
});

$(cloneInput).on('click', '#clone', function(e) {
    e.preventDefault();
    $(this).parent('#addProducts').each(function() {
        $(this).clone().appendTo('div#products').val($(this).val());
    });
    sumPrice();
});

$(cloneInput).on('click', '#clone', function(e) {
    e.preventDefault();
    $(this).parent('#addProducts2').each(function() {
        $(this).clone().appendTo('div#products').val($(this).val());
    });
    sumPrice();
});

File 2 - Javascript file which makes autocomplete working

var ac_config2 = {
source: "../../libs/orders/autocomplete_products.php",
select: function(event, ui){
    $("#tyreID").val(ui.item.id_product);
    $("#tyreSize").val(ui.item.Szerokosc+"/"+ui.item.Profil+"R"+ui.item.Srednica);
    $("#tyreManufacturer").val(ui.item.Producent);
    $("#tyreTread").val(ui.item.Model);
    $("#tyreDOT").val(ui.item.DOT);
    //$("#tyrePricePiece").val(ui.item.client_name);
    },
minLength: 1
};
$("#tyreID").autocomplete(ac_config2);

If I add productbox to html file autocomplete works great, but at dynamically generated div it not works at all.

How can i reload this autocomplete file on each div add? Or there is another way of this?

Still got problem with 1 thing. If I setup it to #div id it copies values always to first div, if to .div class it copies values to all divs.

@Justas?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM