Case-insensitive switch-case

孤人 提交于 2020-12-25 00:38:45

问题


OK, so let's say I have this:

$(function() {
  $('#good_evening').keyup(function () {
    switch($(this).val()) {
    case 'Test':
      // DO STUFF HERE
      break;
    }
  });
});

... this would only run if you typed "Test" and not "test" or "TEST". How do I make it case-insensitive for JavaScript functions?


回答1:


switch($(this).val().toLowerCase()) {
    case 'test':
    // DO STUFF HERE          
    break;
}



回答2:


Why not lowercase the value, and check against lowercase inside your switch statement?

$(function() {
    $('#good_evening').keyup(function () {
        switch($(this).val().toLowerCase()) {
        case 'test':
        // DO STUFF HERE
        break;
        }
    });
});



回答3:


Convert it to upper case. I believe this is how it is done, correct me if I am wrong... (dont -1 me =D )

$(function() {
    $('#good_evening').keyup(function () {
            switch($(this).val().toUpperCase()) {
            case 'TEST':
            // DO STUFF HERE
            break;
        }
    });
});


来源:https://stackoverflow.com/questions/3690186/case-insensitive-switch-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!