简体   繁体   English

缩短if else语句

[英]shorten down if else statement

I am working on a school project and have made this code: 我正在做一个学校项目,并编写了以下代码:

$("#pakke1, #pakke2, #pakke3, #pakke4, #pakke5, #pakke6, #pakke7, #pakke8, #pakke9, #pakke10, #pakke11, #pakke12").click(function() {
    if($(this).is('#pakke1')){
        $("#modal1").css("display","inline-block");  
    }
    else if($(this).is("#pakke2")){
        $("#modal2").css("display","inline-block");  
    }
    else if($(this).is("#pakke3")){
        $("#modal3").css("display","inline-block");  
    }
    else if($(this).is("#pakke4")){
        $("#modal4").css("display","inline-block");  
    }
    else if($(this).is("#pakke5")){
        $("#modal5").css("display","inline-block");  
    }
    else if($(this).is("#pakke6")){
        $("#modal6").css("display","inline-block");  
    }
    else if($(this).is("#pakke7")){
        $("#modal7").css("display","inline-block");  
    }
    else if($(this).is("#pakke8")){
        $("#modal8").css("display","inline-block");  
    }
    else if($(this).is("#pakke9")){
        $("#modal9").css("display","inline-block");  
    }
    else if($(this).is("#pakke10")){
        $("#modal10").css("display","inline-block");  
    }
    else if($(this).is("#pakke11")){
        $("#modal11").css("display","inline-block");  
    }
    else if($(this).is("#pakke12")){
        $("#modal12").css("display","inline-block");  
    }
});

and I am having a hard time shortening it down so it can connect the #pakke id to the right #modal id when clicked. 而且我很难缩短它的时间,因此单击时它可以将#pakke ID连接到正确的#modal ID。 The more simple the better, I have very limited knowledge of working with arrays. 越简单越好,我对数组的了解非常有限。 hope someone can help me as my deadline is closing in. 希望有人在我的截止日期临近之前为我提供帮助。

This should do the trick for you since you want to "match" pakke1 with modal1 and so on. 这应该为您解决问题,因为您想使pakke1与modal1等“匹配”。

So we take the id with .attr("id") and remove "pakke" so we are left with the number. 因此,我们采取id.attr("id")所以我们留下了数量删除“pakke”。

Then you can add the number to $("#modal") like this $("#modal" + number) 然后,您可以将数字添加到$("#modal")这样的$("#modal" + number)

 $("[id^='pakke']").click(function() { var number = $(this).attr("id").replace("pakke", ""); $("#modal" + number).css("display", "inline-block"); }); 

Slightly different approach, you can use regex to just extract the number from the string. 略有不同的方法是,您可以使用正则表达式从字符串中提取数字。

var number = $(this).attr("id").replace(/^\D+/g, '');
$("#modal" + number).css("display", "inline-block");

Tip: using replace instead of match as in case id does not have a number, it will throw an exception. 提示:如果id没有数字,请使用replace而不是match ,它将引发异常。

You can make use of jquery start with selector where you can bind click event for all elements starting id with pakke , see below code 您可以使用以选择器开头的jquery,可以在其中绑定所有以id为pakke元素的click事件pakke ,请参见以下代码

$("[id^=pakke").click(function() {//bind click handler for all having id start with pakke
  var number = $(this).attr("id").replace("pakke", "");
  $("#modal" + number).show();//use show() method instead of display 
});

This will help you if there is increase or decrease in number of elements having id start with pakke 如果id以pakke开头的元素数量增加或减少,这将为您提供pakke

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM