the following code should display the charcode of keys pressed including enter (13) but it doesn't in IE. In IE8 / IE9 / IE11 (and probably IE10 but I don't have it somewhere right now) it moves focus to another element when I press the enter key. Why and how to solve this ? I could use onkeydown, but why doesn't work with onkeyup as well as in all other browsers?
I should say that this will not happen if I remove the button element or the input type image but obviously a page will have many such elements.
<html>
<head>
<script type="text/javascript">
function onuserinput(e) {
var keyC = e.keyCode ? e.keyCode : e.charCode;
alert(keyC);
}
</script>
</head>
<body>
<input type="text" id="myinput" onkeyup="onuserinput(event);"/>
<input type="image" id="img" src="" alt="random input type image" title=""/>
<button id="aaa">random button</button>
</body>
</html>
Try onkeyup , it should work. As I understand onuserinput fires after pressing enter or losing focus, so keyup must fires before.
I had the same problem. IE default behavior, it seems, is submit the nearest button in the "form". For elude this behavior, include type="button" propierty to the html button, it works for me:
<button type='button'>This button will not submit by default</button>
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.