有时候你希望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;
本文介绍了一种通过JavaScript阻止表单默认提交行为的方法,特别是如何防止按下回车键时表单自动提交,这对于需要定制表单提交逻辑的情况非常有用。
701

被折叠的 条评论
为什么被折叠?



