If I have an unknown amount of identifiers sharing a specific naming-scheme, is there a way to grab them all at once using jQuery?

// These are the IDs I'd like to select
#instance1
#instance2
#instance3
#instance4

// What do I need to add or how do I need to modify this jQuery selector in order to select all the IDs above?
("#instanceWILDCARD").click(function(){}

推荐答案

The attribute starts-with selector ('^=) will work for your IDs, like this:

$("[id^=instance]").click(function() {
  //do stuff
});

However, consider giving your elements a common class, for instance (I crack myself up) .instance, and use that selector:

$(".instance").click(function() {
  //do stuff
});

Jquery相关问答推荐

将选定2个AJAX结果显示到模板中

更改高亮 colored颜色

如何使用 jquery 获取屏幕的高度

jQuery从字符串中删除字符串

jQuery 的 ajax 默认超时值是多少?

错误找不到 jquery-2.0.2.min.map

JSON字符串到JS对象

找到下一个匹配的sibling 姐妹的有效,简洁的方法?

JSLint 消息:未使用的变量

按百分比zoom div的内容?

将返回的 JSON 对象属性转换为(较低的第一个)camelCase

jQuery 如果 div 包含此文本,则替换该部分文本

如何使用 javascript (jquery) 将整数值添加到返回字符串的值中?

带有回调ajax json的jQuery自动完成

在未实现接口 FormData 的对象上调用附加

jQuery attr 与props ?

使用 jQuery 和 CSS 将数字转换为星级显示

为什么我应该创建异步 WebAPI 操作而不是同步操作?

如何使用 jQuery 处理复选框的更改?

jQuery:通过 .attr() 添加两个属性;方法