简体   繁体   中英

not working: <form onsubmit=“alert(event.shiftKey)”> on javascript

Problem: I can not check whether the shift Key was being held on a form submit. This returns an undefined instead of true or false .
Why this happens?

Example of the problem: http://jsfiddle.net/DRSDavidSoft/fFYKs/

What I want: I want to know whether the shift key was being held when a form is submitted.
Thanks for you help.

This is because submit event know nothing about keys. I'd advice to replace your submit input with button input. And there check will work fine. Here is the sample: http://jsfiddle.net/fFYKs/2/

<input type="button" value="Click here to submit" onclick='isPressed(event);'/>

And JS:

function isPressed(e){
    if (e.shiftKey) {
        window.myForm.submit();
    }
}

Also for cross-platform details recommend to visit this link: http://goo.gl/wKdJMO

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