Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使大小写函数不区分大小写? 函数重定向(){ var input=document.getElementById('userInput')。值; 开关(输入){ 美国铝业案: window.location.replace('alcoa-Forms.htm'); 打破 美国铝业案: window.location.replace('/alcoa Forms.htm'); 打破_Javascript_Case - Fatal编程技术网

Javascript 如何使大小写函数不区分大小写? 函数重定向(){ var input=document.getElementById('userInput')。值; 开关(输入){ 美国铝业案: window.location.replace('alcoa-Forms.htm'); 打破 美国铝业案: window.location.replace('/alcoa Forms.htm'); 打破

Javascript 如何使大小写函数不区分大小写? 函数重定向(){ var input=document.getElementById('userInput')。值; 开关(输入){ 美国铝业案: window.location.replace('alcoa-Forms.htm'); 打破 美国铝业案: window.location.replace('/alcoa Forms.htm'); 打破,javascript,case,Javascript,Case,如何使此函数不区分大小写,以便只编写一次?最简单的方法是将输入全部大写或小写。选择: <script type='text/javascript'> function redirect() { var input = document.getElementById('userInput').value; switch(input) { case 'ALCOA':

如何使此函数不区分大小写,以便只编写一次?

最简单的方法是将输入全部大写或小写。选择:

        <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;
请记住,这也适用于美国铝业、美国铝业等

您也可以只写两次
case

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

您需要将输入转换为小写或大写。例如:

switch (input) {
   case 'ALCOA':
   case 'alcoa':
使用.toLowerCase()或.toLocalLowerCase()命令 请注意,这些函数几乎相同,但在土耳其语等语言中有一些模糊的例外

代码 更详细 函数不区分大小写。相反,代码区分大小写。避免此问题的方法是在检查结果之前将输入规范化为单个大小写。一种方法是在检查之前将字符串转换为所有小写

另一种解决办法 使用case fallthrough语法:

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

使输入大写:

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

函数重定向(){
var input=document.getElementById('userInput')。值;
input=input.toUpperCase()
开关(输入){
美国铝业案:
window.location.replace('alcoa-Forms.htm');
打破
虽然
.toLowerCase()
(或
.toUpperCase()
)是最简单的方法,但还有一种正则表达式方法:

<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;
如果使用toUpperCase(),则使用内部开关(输入)函数 大小写字符串应全部为大写,如下所示:

if (/^alcoa$/i.test(input)) {
    // ...
}
   var input = document.getElementById('userInput').value.toUpperCase();
    switch (input) {
       case 'ALCOA':
               // do something
               break;
    }
如果使用toLowerCase(),则内部开关(输入)函数 大小写字符串应全部使用小写,如下所示:

if (/^alcoa$/i.test(input)) {
    // ...
}
   var input = document.getElementById('userInput').value.toUpperCase();
    switch (input) {
       case 'ALCOA':
               // do something
               break;
    }

谢谢。我会让你知道的。我想没有人意识到,整个好处是使案例选项像camel case一样可读,但又使它们不敏感。我想没有办法做到这一点。