1

i'm trying to use Jquery autocomplete function getting data from a multidimensional array (see the code below), i want that user write on the form field called "descrizione" getting results from the array field "prodotto" and automatically when he confirm the product chosen the other fields are set. Anyone can help me to understand what is wrong on the code? Thanks for your kind cooperation!

<script>
$(function() {
    var availableDescrizioni = [
{ prodotto: "test test", codice_prodotto: "product_code001", prezzo: "122,22", iva: "22" },
{ prodotto: "nome_prodotto1", codice_prodotto: "cod_produttore1", prezzo: "22,22", iva: "22" }

    ];

    $("#descrizione").autocomplete({
        source: $availableDescrizioni,

        focus: function(event, ui) {
            $("#descrizione").val(ui.item.prodotto);
            return false;
        },
        select: function(event, ui) {
            $("#descrizione").val(ui.item.prodotto);
            $("#cod_prodotto").val(ui.item.codice_prodotto);
            $("#prezzo_unitario").val(ui.item.prezzo);
            return false;
        }
    });



  });
</script>


<input type="text" id="cod_prodotto" name="cod_prodotto" class="colonna col_codprodotto"/>
<input type="text" name="quantita" class="colonna col_quantita"/>
<input type="text" id="descrizione" name="descrizione" class="colonna col_descrizione"/> 
<input type="text" id="prezzo_unitario" name="prezzo_unitario" class="colonna col_prezzo"/> 
<input type="text" name="cod_iva" class="colonna col_prezzo"/>
1

1 Answer 1

1

Here are the problems within your code :

  • your availableDescrizioni variable is not in a valid format to be the source of the autocomplete widget. If you want to use an Objects array, each object must contains at least a value property which is used by the widget to filter results. (see the doc for more details)

  • you have an extra $ sign on this line : source: $availableDescrizioni,

Have a look in this jsFiddle to see it in action

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your precious help! i tried many times but i didn't know that value property was mandatory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.