简体   繁体   中英

Javascript array for auto-complete

I implemented a auto-complete function that takes an array as parameter.

$(document).ready(function(){
    $('#empf').autocomplete(['black', 'white', 'red']);
}

Now I do not want a static array . Outside the document ready function I declared a function that retrieves the colors from local storage. I call this function in document ready function.

So everytime the user enters new input I want to put it in the array and use that array globally . Is that possible?

For initialisation of the array, at any point I know the number of colores stored.

So instead of the static array I put a variable eg data and I declare data as an array. I tried it this way:

var colors;

$(document).ready(function(){
        loadColors();
        $('#empf').autocomplete(colors);
}

function loadColors(){
 colors = new Array(getNumColor()));
//in a loop save the colors to array using colors[i] = ...
}

But this causes my application to crash. Any ideas?

Any ideas?

Like Rahul said this seems to be an issue of parenthesis. With firebug ( another link ) it is easier to detect/debug these bugs.

Also I think you should use jquery ui's autocomplete if you are not already using it(I am not sure if you are using it).

Seems to be an issue with parenthesis.

It should be

colors = new Array(getNumColor());

instead of

colors = new Array(getNumColor()));

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