我有多个具有相同前缀名称的数组。
var region1 = ['01','02','03','04','05','06','07'];
var region2 = ['08','09','11','12'];
var region3 = ['13'];
var region4 = ['14','22'];
var region5 = ['10','15','16','17','18','19','20'];
var region6 = ['21','23','24'];
var region7 = ['25','26','27','28','29','30'];
var region8 = ['31','33','36','37','38','39'];
var region9 = ['32','34','35','40','41','42','43','44','45','46','47'];
var prefecture = "33";
根据县值我想显示区域
if(region1.indexOf(prefecture) != -1){
$('.region1').show();
$('a.region1').addClass('regionSelect');
}
else if(region2.indexOf(prefecture) != -1){
$('.region2').show();
$('a.region2').addClass('regionSelect');
}
else if(region3.indexOf(prefecture) != -1){
$('.region3').show();
$('a.region3').addClass('regionSelect');
}
else if(region4.indexOf(prefecture) != -1){
$('.region4').show();
$('a.region4').addClass('regionSelect');
}
else if(region5.indexOf(prefecture) != -1){
$('.region5').show();
$('a.region5').addClass('regionSelect');
}
else if(region6.indexOf(prefecture) != -1){
$('.region6').show();
$('a.region6').addClass('regionSelect');
}
else if(region7.indexOf(prefecture) != -1){
$('.region7').show();
$('a.region7').addClass('regionSelect');
}
else if(region8.indexOf(prefecture) != -1){
$('.region8').show();
$('a.region8').addClass('regionSelect');
}
这工作正常
但是我尝试这样来最小化代码。
for(var i=1; i<=9; i++){
if('region'+i.indexOf(prefecture) != -1){
$('.region'+i).show();
$('.region'+i).addClass('regionSelect');
}
}
它说 i.indexOf 没有定义