我有以下代码,我想将其压缩为一个 for 循环,但不确定如何。我尝试查看其他帖子,但被卡住了。
总共有 15 个数字,所以类似于 for (i = 1; i <= 15; i++)
if(checksquare1save=="true"){
$("#checksquare1").toggleClass("checksquareON");
}
else{
$("#checksquare1").removeClass("checksquareON");
}
if(checksquare2save=="true"){
$("#checksquare2").toggleClass("checksquareON");
}
else{
$("#checksquare2").removeClass("checksquareON");
}
if(checksquare3save=="true"){
$("#checksquare3").toggleClass("checksquareON");
}
else{
$("#checksquare3").removeClass("checksquareON");
}
if(checksquare4save=="true"){
$("#checksquare4").toggleClass("checksquareON");
}
else{
$("#checksquare4").removeClass("checksquareON");
}
if(checksquare5save=="true"){
$("#checksquare5").toggleClass("checksquareON");
}
else{
$("#checksquare5").removeClass("checksquareON");
}
if(checksquare6save=="true"){
$("#checksquare6").toggleClass("checksquareON");
}
else{
$("#checksquare6").removeClass("checksquareON");
}
if(checksquare7save=="true"){
$("#checksquare7").toggleClass("checksquareON");
}
else{
$("#checksquare7").removeClass("checksquareON");
}
if(checksquare8save=="true"){
$("#checksquare8").toggleClass("checksquareON");
}
else{
$("#checksquare8").removeClass("checksquareON");
}
if(checksquare9save=="true"){
$("#checksquare9").toggleClass("checksquareON");
}
else{
$("#checksquare9").removeClass("checksquareON");
}
if(checksquare10save=="true"){
$("#checksquare10").toggleClass("checksquareON");
}
else{
$("#checksquare10").removeClass("checksquareON");
}
if(checksquare11save=="true"){
$("#checksquare11").toggleClass("checksquareON");
}
else{
$("#checksquare11").removeClass("checksquareON");
}
if(checksquare12save=="true"){
$("#checksquare12").toggleClass("checksquareON");
}
else{
$("#checksquare12").removeClass("checksquareON");
}
if(checksquare13save=="true"){
$("#checksquare13").toggleClass("checksquareON");
}
else{
$("#checksquare13").removeClass("checksquareON");
}
if(checksquare14save=="true"){
$("#checksquare14").toggleClass("checksquareON");
}
else{
$("#checksquare14").removeClass("checksquareON");
}
if(checksquare15save=="true"){
$("#checksquare15").toggleClass("checksquareON");
}
else{
$("#checksquare15").removeClass("checksquareON");
}
我确实尝试过,但没有用
for (i = 1; i <= 15; i++){
if(checksquare + i + save=="true"){
$("#checksquare" + i).toggleClass("checksquareON");
}
}
编辑:
应该提到我上面有这些变量
checksquare1save = localStorage['ScienceSkills-basicSkillsCheck-check1-7868-2171-1085-5119-4672']
checksquare2save = localStorage['ScienceSkills-basicSkillsCheck-check2-6582-7209-6435-8487-6994']
checksquare3save = localStorage['ScienceSkills-basicSkillsCheck-check3-5367-9000-3502-5810-2295']
checksquare4save = localStorage['ScienceSkills-basicSkillsCheck-check4-2961-8359-5106-2702-5132']
checksquare5save = localStorage['ScienceSkills-basicSkillsCheck-check5-5801-5256-5838-2934-5277']
checksquare6save = localStorage['ScienceSkills-basicSkillsCheck-check6-1162-2208-7274-5156-3693']
checksquare7save = localStorage['ScienceSkills-basicSkillsCheck-check7-6178-4335-3148-1809-8066']
checksquare8save = localStorage['ScienceSkills-basicSkillsCheck-check8-6609-6032-4539-4243-6273']
checksquare9save = localStorage['ScienceSkills-basicSkillsCheck-check9-6261-3580-7658-7073-1914']
checksquare10save = localStorage['ScienceSkills-basicSkillsCheck-check10-1504-7246-1864-4465-1319']
checksquare11save = localStorage['ScienceSkills-basicSkillsCheck-check11-6400-3954-7124-3585-2086']
checksquare12save = localStorage['ScienceSkills-basicSkillsCheck-check12-8303-8392-7301-5154-8007']
checksquare13save = localStorage['ScienceSkills-basicSkillsCheck-check13-1207-8834-5941-3315-8893']
checksquare14save = localStorage['ScienceSkills-basicSkillsCheck-check14-3089-4036-1427-2614-7399']
checksquare15save = localStorage['ScienceSkills-basicSkillsCheck-check15-2750-4976-5357-6403-2979']