简体   繁体   中英

If-statement triggering despite not every condition met

I've got this if condition:

(48 - 57 are the typewriter numerics, 96 - 105 apply to the numeric keypad)

  if((e.ctrlKey && e.altKey) && ((e.keyCode > 47 && e.keyCode < 58)) || (e.keyCode > 95 && e.keyCode < 106)){
      console.log(e.altKey);
  }

This is working well if i do not use the numeric keypad but the typewriter keys instead.

However, if i press ctrl + any number on the numeric keypad, the condition will trigger and output false to the console

How can that be?

你的意思是?

(e.ctrlKey && e.altKey) && ((e.keyCode > 47 && e.keyCode < 58) || (e.keyCode > 95 && e.keyCode < 106))

Can you try this :

if  (
        ( e.ctrlKey && e.altKey ) 
        && (
            (e.keyCode > 47 && e.keyCode < 58) 
            || (e.keyCode > 95 && e.keyCode < 106)
        )
    )
{
      console.log(e.altKey);
}

You had a ) after < 58 wich was not on good place

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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