Javascript 基于文本输入的重定向不区分大小写

Javascript 基于文本输入的重定向不区分大小写,javascript,Javascript,这里是新的,对javascript来说是全新的 我从这个网站上抓到了这段代码,我想,我不记得在哪里了 <form id='formName' name='formName' onsubmit='redirect();return false;'> <input type='text' id='userInput' name='userInput' value=''> <input type='submit' name

这里是新的,对javascript来说是全新的

我从这个网站上抓到了这段代码,我想,我不记得在哪里了

 <form id='formName' name='formName' onsubmit='redirect();return false;'>
            <input type='text' id='userInput' name='userInput' value=''>
            <input type='submit' name='submit' value='Submit'>
        </form>

        <script type='text/javascript'>
        function redirect() {
            var input = document.getElementById('userInput').value;
            switch(input) {
                case 'keyword1':
                    window.location.replace('page1.html');
                    break;
                case 'keyword2':
                    window.location.replace('page2.html');
                    break;
                default:
                    window.location.replace('error.html');
                    break;
            }


        }
        </script>

函数重定向(){
var input=document.getElementById('userInput')。值;
开关(输入){
案例“关键字1”:
window.location.replace('page1.html');
打破
案例“关键字2”:
window.location.replace('page2.html');
打破
违约:
window.location.replace('error.html');
打破
}
}
现在,只有当有人键入“关键字1”而不是“关键字1”或“关键字1”等时,它才会起作用。我确信解决这个问题很简单,但正如我所说,我是新的:-p


谢谢。

将输入行替换为:

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

将输入行替换为:

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

在进行切换之前,可以将字符串转换为小写:

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

在进行切换之前,可以将字符串转换为小写:

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

你试过什么吗?你想让它接受变化吗?你试过什么吗?你想让它接受变化吗?谢谢,我知道它是这样的,只是不知道它去了哪里。谢谢,我知道它是那样的,只是不知道它去了哪里。