2

我有一系列选择:

var options = ["foo_A", "foo_B", "foo_C", "foo_D"];

...当用户单击以下元素之一时...

<div id="bar_A"> </div>
<div id="bar_B"> </div>
<div id="bar_C"> </div>
<div id="bar_D"> </div>

...它设置以下变量:

var currentBar = $(this).attr('id');

...然后我想搜索 options 数组并找到后缀与 currentBar 匹配的项目,如下所示:

 $.each(options, function(i,v){
 if (v's suffix matches the currentBar's suffix){

 //function to do something

}
4

2 回答 2

3

您需要split在 currentBar 上使用函数

var Extract = currentBar.split('_');
// Extract[0] will be bar and Extract[1] will be the letter

之后,第二个值将是字母并将匹配。

var Key = indexOf('foo_' + Extract[1], options);

alert(options[Key]);
于 2012-06-27T16:51:25.103 回答
3
$.each(options, function(i,v){
    if (options[i].split("_")[1] == currentBar.split("_")[1]){

   //function to do something

}
于 2012-06-27T16:54:30.097 回答