I have placed the following code at the top of my page in the head:
<script type="text/javascript">
function stopRKey(evt) {
console.log(evt.keyCode);
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((ev开发者_开发问答t.srcElement) ? evt.srcElement : null);
console.log(evt.keyCode);
if ((evt.keyCode == 13) && (node.type == "text")) {
return false;
}
}
document.onkeypress = stopRKey;
</script>
On one of my pages it is not firing the event when I hit enter. But only one one of my pages the reast seem to work and log a keycode of 13 thus stopping the postback.
Any ideas why this event wouldnt be firing on this certain page?
Cheers, Pete
You can certainly simplify the code. There's no reason why this shouldn't work, for <input type="text">
elements:
function stopRKey(evt) {
evt = evt || window.event;
var node = evt.target || evt.srcElement;
console.log(evt.keyCode);
if (evt.keyCode == 13 && node.nodeName == "INPUT" && node.type == "text") {
return false;
}
}
Perhaps there form element on your broken page(s) triggering the postback that is not a text node. Try removing the second part of the condition that prevents the submit, to see what happens:
if (evt.keyCode == 13) {
return false;
}
精彩评论