2016-01-23 69 views
3

我有6个HTML按钮。我正在使用敲除绑定和索引他们不同。他们每个人的身份证号码如下:Jquery:检查属性ID是否具有特定模式

btnFood-0 
btnFood-1 
btnFood-2 
btnFood-3 
btnFood-4 
btnFood-5 

我想唯一标识是否点击是由这些触发的?

我试过,

($(this).attr(id) == btnFood-0) OR ($(this).attr(id) == btnFood-1) 
OR ($(this).attr(id) == btnFood-2) OR ($(this).attr(id) == btnFood-3) 
OR ($(this).attr(id) == btnFood-4) OR ($(this).attr(id) == btnFood-5) 

但它不是一个很好的解决方案。我想用正则表达式来识别它来自($(this).attr(id) == btnFood-*)源。

我该怎么做?

回答

2

你可以只使用indexOf

if (this.id.indexOf('btnFood-') === 0) { 
    // do stuff 
} 

或专门

$('[id^="btnFood-"]').on('click' ... 
0

针对他们这将工作

if($(this).attr("id").indexOf("btnFood")>=0){ 
    //YOUR CODE 
}