问题
I'm trying to use the jQuery autocomplete plugin. I have an array of IDs that I want to plugin to work on. So, say I have:
var aIds = ["1", "2"];
The examples I see on how to use the plugin looks like this:
$('#1').autocomplete
Is there a way for me to use this autocomplete plugin and my array of ids? The array of ids are coming from a web service.
Also, the autocomplete plugin exposes certain events like select
(see: http://docs.jquery.com/UI/Autocomplete#event-select). When that happens, how can I tell which element triggered the event (if I am assigning the array of ids dynamically)?
回答1:
If you want to make a selector:
$('#' + aIds.join(', #')).autocomplete();
回答2:
for(i=0;i<aIds.length;i++)
$('#'+aIds[i]).autocomplete();
回答3:
Easy, just need to join them together and pass it in. Jquery is great and lets you pass in multiple selectors.
var aIds = ["1", "2"];
// join together your IDS
var selectors = "#" + aIds.join(",#");
// pass in as selectors
$(selectors).autocomplete
来源:https://stackoverflow.com/questions/7343195/using-an-array-of-css-selectors-in-jquery