有时候你希望form表单的提交 通过自己的事件提交,而不需要按回车键就自动提交,甚至,有时对你来说按回车键提交了还会产生错误或你不想要的结果,这是该怎么处理呢?可以通过截获键盘事件,判断到是回车的时候返回false.
var Nav4 = document.layers;
var IE4 = document.all;
function enterkey(e)
{
//alert("In EnterKey function");
if (Nav4)
{
keyPressed = String.fromCharCode(e.which);
}
else if (IE4)
{
// alert("In IE4");
keyPressed = String.fromCharCode(window.event.keyCode);
}
if(keyPressed == "/r" || keyPressed == "/n")
{
// alert("In keypress");
return (false);
}
}
if (window.document.captureEvents != null)
window.document.captureEvents(Event.KEYPRESS)
window.document.onkeypress = enterkey;