简体   繁体   English

在jQuery中使用CSS选择器数组

[英]Using an array of CSS selectors in jQuery

I'm trying to use the jQuery autocomplete plugin. 我正在尝试使用jQuery自动完成插件。 I have an array of IDs that I want to plugin to work on. 我有一个要插入的ID数组。 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? 有没有办法让我使用此自动完成插件和ID数组? The array of ids are coming from a web service. ID数组来自Web服务。

Also, the autocomplete plugin exposes certain events like select (see: http://docs.jquery.com/UI/Autocomplete#event-select ). 另外,自动完成插件会公开某些事件,例如select (请参阅: 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)? 发生这种情况时,如何判断哪个元素触发了事件(如果我要动态分配ID数组)?

如果要选择一个:

$('#' + aIds.join(', #')).autocomplete();

for(i=0;i<aIds.length;i++)
$('#'+aIds[i]).autocomplete();

Easy, just need to join them together and pass it in. Jquery is great and lets you pass in multiple selectors. 简单,只需要将它们连接在一起并传递进去。jQuery非常棒,可以让您传递多个选择器。

   var aIds = ["1", "2"];

    // join together your IDS
    var selectors = "#" + aIds.join(",#");

    // pass in as selectors
    $(selectors).autocomplete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM