JavaScript window.onsubmit event.keyCode undefined

时间:2015-01-14 15:31:53

标签: javascript

我正在迁移远离Microsoft Access的客户的员工已经习惯通过表单字段按EnterTab,尽管如此,这种情况很奇怪。所以我需要在preventDefault事件上window.onsubmit,只定义event.keyCode

window.onsubmit = function(event)
{
 alert('event.keyCode = '+event.keyCode);
}

以下是JavaScript在event参数上返回的方法,对象和属性的列表......

window.onsubmit.event.isTrusted
window.onsubmit.event.stopPropagation
window.onsubmit.event.stopImmediatePropagation
window.onsubmit.event.preventDefault
window.onsubmit.event.initEvent
window.onsubmit.event.getPreventDefault
window.onsubmit.event.type
window.onsubmit.event.target
window.onsubmit.event.currentTarget
window.onsubmit.event.eventPhase
window.onsubmit.event.bubbles
window.onsubmit.event.cancelable
window.onsubmit.event.defaultPrevented
window.onsubmit.event.timeStamp
window.onsubmit.event.originalTarget
window.onsubmit.event.explicitOriginalTarget
window.onsubmit.event.NONE
window.onsubmit.event.CAPTURING_PHASE
window.onsubmit.event.AT_TARGET
window.onsubmit.event.BUBBLING_PHASE
window.onsubmit.event.ALT_MASK
window.onsubmit.event.CONTROL_MASK
window.onsubmit.event.SHIFT_MASK
window.onsubmit.event.META_MASK

1 个答案:

答案 0 :(得分:0)

除了ondemand JavaScript我的工作涉及(服务器编译)index.jsevents.js,所有window.onevent处理程序都存储在events.js文件中。虽然它显然不是直接可能的,但仍然间接可能阻止Enter密钥提交表单。此外,keypress事件似乎会触发click事件,可能是出于辅助目的。我还包括一个解决方案,以确定哪个事件首先发生;触发click事件后,每个浏览器在0~20ms之间触发keypress事件,因此我考虑确保范围为0~50ms。以下是事件触发器和代码之间的浏览器响应时间......

Chrome 39.0

Firefox 31.0 ESR:18~20ms

IE 11 :2~4ms

Opera 12.1 :0ms

var option = new function() {this.name = '';}


window.onclick = function(event)
{
 option.clickDate = new Date().getTime();
}


window.onkeypress = function(event)
{
 option.keyCode = event.keyCode;
 option.keyCodeDate = new Date().getTime();
}


window.onsubmit = function(event)
{
 if (option.keyCodeDate && option.clickDate && 
 ((option.clickDate - option.keyCodeDate)>=0 && 
 (option.clickDate - option.keyCodeDate)<51) && 
 option.keyCode==13) {event.preventDefault();}
 {
  event.preventDefault();
 }
}