1

我有以下代码,我想将其压缩为一个 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'] 
4

5 回答 5

5

如果您的变量在全局范围内,这应该可以工作:

    for (i = 1; i <= 15; i++){
        if(window['checksquare' + i + 'save']=="true"){
            $("#checksquare" + i).toggleClass("checksquareON");
        }
        else{
            $("#checksquare" + i).removeClass("checksquareON");
        }
    }   
于 2013-10-30T13:34:31.877 回答
4

你最好使用一个数组checksquaresavechecksquareXsave然后像你正在做的那样循环数组。它需要一些重构,但它会在以后为您节省一些痛苦。

for (i = 1; i <= 15; i++){
   if(checksquare[i] == 'true'){
       $("#checksquare" + i).toggleClass("checksquareON");
   }
}
于 2013-10-30T13:33:46.147 回答
1

您现在正在执行的代码将检查 3 个变量的值:checksquare、i & save,而不是一个。

更好的方法是将所有数据放在一个数组中并通过它。

如果你有一个数组:checksquaresave。那么你可以用你的价值观来填充它:

checksquaresave[0] =value0, checksquaresave[1] =value1 , ... 

在此之后,代码将很简单:

for (i = 1; i <= 15; i++){
    if(checksquaresave[i] =="true"){
        $("#checksquare" + i).toggleClass("checksquareON");
    }   
}  

编辑:正如@Archer 提到的(以及其他答案),您可以使用

window["checksquare" + i + "save"] 

也是,但我认为最好的方法是只使用一个数组。(cfr.在 javascript 中使用 window["variable" + i]

于 2013-10-30T13:31:51.623 回答
1

显示您拥有的代码时,我能想到的唯一解决方案涉及使用eval. 忘了我说过,因为你不想尝试那个解决方案。拥有 15 个名称相似的变量可能是个问题。如果“checksquaresave”变量是全局变量,您可能能够使用window.variableName或检索它们。window["variableName"]在这种情况下,它看起来像:

if (window["checksquare" + i + "save"] == "true")

否则,最好检查 checksquare##save 变量的创建,并将它们设置为数组而不是单独的变量。然后,您的代码可能如下所示:

    for (i = 1; i <= 15; i++){
        if(checksquaresave[i] =="true"){
            $("#checksquare" + i).toggleClass("checksquareON");
        }   
    }   
于 2013-10-30T13:35:48.123 回答
1

如果要访问仅在运行时才知道名称的变量,可以将它们存储为对象的属性。

所以如果你想访问 checksquare1save、checksquare2save 等,你可以这样做:

var storage = {
    checksquare0save:true,
    checksquare1save:true,
    checksquare2save:false,
    //...other variables...
}

for (var i = 0; i < 15, i++) {
    console.log(storage["checksquare" + i + "save"])
}
于 2013-10-30T13:36:07.133 回答