简体   繁体   English

如何使案例功能不区分大小写?

[英]How do I make case function non case sensitive?

        <script type='text/javascript'>
        function redirect() {
            var input = document.getElementById('userInput').value;
            switch(input) {
                case 'ALCOA':
                    window.location.replace('alcoa-Forms.htm');
                    break;
                case 'alcoa':
                    window.location.replace('/alcoa-Forms.htm');    
                    break;

How do I make it so this function is not case sensitive so I can just write it once? 我怎么做到这个功能不区分大小写,所以我可以写一次?

simplest way is to make the input all upper or all lower case. 最简单的方法是使输入全部为大写或全部为小写。 Take your pick: 拿你的选择:

input = input.toUpperCase();
switch (input) {
   case 'ALCOA':
      ...

Keep in mind that this will also work for Alcoa , aLcOa , etc. 请记住,这也适用于AlcoaaLcOa等。

You could also just write case twice: 你也可以写两次case

switch (input) {
   case 'ALCOA':
   case 'alcoa':

make the input uppercase: 使输入大写:

<script type='text/javascript'>
    function redirect() {
        var input = document.getElementById('userInput').value;
        input = input.toUpperCase()
        switch(input) {
            case 'ALCOA':
                window.location.replace('alcoa-Forms.htm');
                break;

You need to convert your input to either lower or upper case. 您需要将输入转换为小写或大写。 For example: 例如:

var input = document.getElementById('userInput').value.toLowerCase();

Use .toLowerCase() or .toLocaleLowerCase() Note that these functions are nearly identical with some obscure exceptions in languages such as Turkish. 使用.toLowerCase()或.toLocaleLowerCase()请注意,这些函数几乎与土耳其语等语言中的一些模糊异常相同。

Code

function redirect() {
     var input = document.getElementById('userInput').value.toLowerCase();
     switch (input) {
        case 'alcoa':
            window.location.replace('alcoa-Forms.htm');
            break;
    }
}

More detailed 更详细

A function is not "case sensitive". 功能不是“区分大小写”。 Rather, your code is case sensitive. 相反, 您的代码区分大小写。 The way to avoid this problem is to normalize the input to a single case before checking the results. 避免此问题的方法是在检查结果之前将输入规范化为单个案例。 One way of doing so is to turn the string into all lowercase before checking. 这样做的一种方法是在检查之前将字符串全部变为小写。

An alternate solution 另一种解决方案

Use the case fallthrough syntax: 使用案例通过语法:

switch(text) { 
     case 'a':
     case 'A':
         doSomething();
}

Though .toLowerCase() (or .toUpperCase() ) is the simplest way, there is also a regex way: 尽管.toLowerCase() (或.toUpperCase() )是最简单的方法,但也有一种正则表达式:

if (/^alcoa$/i.test(input)) {
    // ...
}

if you use toUpperCase() then inside switch(input) function case string should be all in upper case like below: 如果你使用toUpperCase()然后在switch(输入)里面,函数case字符串应该是大写的,如下所示:

   var input = document.getElementById('userInput').value.toUpperCase();
    switch (input) {
       case 'ALCOA':
               // do something
               break;
    }

if you use toLowerCase() then inside switch(input) function case string should be all in lower case like below: 如果你使用toLowerCase()那么在switch(输入)里面的函数case字符串应该是小写的,如下所示:

 var input = document.getElementById('userInput').value.toLowerCase();
switch (input) {
   case 'alcoa':
           // do something
           break;
}

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

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