简体   繁体   中英

jquery autocomplete with input array

I am using the following formular:

ROW1: <input class="search" name="pos[]" value=""><input name="str[]" value="">
ROW2: <input class="search" name="pos[]" value=""><input name="str[]" value="">
ROW3: <input class="search" name="pos[]" value=""><input name="str[]" value="">

For the first input of each row I activated jquery autocomplete:

$(".search").autocomplete(ac_new_invoice);
var ac_new_invoice = {
    source: "productsearch.php",
    minLength:1,
    select: function(data){
        var str = document.forms[0].elements["str[]"];
        str[1].value = '1.000,00';
    }
}

So it works fine when I type

 str[1].value = '1.000,00';

But what i want is that if I type in row 1 the input str in row 1 will be changed. But how do I get the row in which the autocomplete is performed from?

Thanks in advance!

Rather than apply autocomplete to all the elements at once, you can call it on each element.

$('.search').each(function (i) {
    var ac_new_invoice = (function (i) {
        return {
            source: "productsearch.php",
            minLength: 1,
            select: function (data) {
                var str = document.forms[0].elements["str[]"];
                str[i].value = '1.000,00';
            }
        };
    })(i);
    $(this).autocomplete(ac_new_invoice);
});

Note that you'll need (function (i){ blabla })(i) to make a immediate copy of the variable i .

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