Using an array of CSS selectors in jQuery

烈酒焚心 提交于 2019-12-11 09:23:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!