onkeydown事件,应用技术巧,onkeydown事件处理回车后提交表单
源地址:http://www.java-asp.net/aspnet/200511/t_33982.html
body onkeydown="if (event.keyCode==13) {document.all.button2.click()}">
下面的五种方法都可以帮你解决这种问题
1.<script language=
javascript>
function document.onkeydown()
{
if (event.keyCode == 13)
{
if (event.srcElement == document.getElementById(´<%=txtEmpCode.ClientID%>´))
document.getElementById(´<%=BtnQry.ClientID%>´).click();
else if (event.srcElement == document.getElementById(´<%=txtDeptCode.ClientID%>´))
document.getElementById(´<%=btnSave.ClientID%>´).click();
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>
2.
<body onkeydown="onkey()" bgColor="#f5efd5" MS_POSITIONING="GridLayout">
<script language="javascript">
function onkey()
{
if (window.event.keyCode==13)
{
document.all["btnQry"].focus();
}
}
</script>
3.
function document.onkeydown()
{
var source=event.srcElement.id
var btnid
if (event.keyCode == 13 )
{
if (source=="txtEmpCode") btnid="btnQry"
else if (source=="txtDeptCode") btnid="btnSave"
alert(btnid);
var btn = document.getElementById(btnid);
if (btn != event.srcElement)
{btn.click();
return false;}
}
}
4.
TextBox1.Attributes.Add("onkeydown", "javascript:if (event.keyCode==13) {document.all." & Button1.ClientID & ".click();return false;}")
5.
<script language="javascript">
<!--
document.onkeydown=GlobalKeyDown;
_CMDRETURN=13;
_CMDTAB = 9;
_CMDDot1 = 190;
_CMDDot2 = 110;
function GlobalKeyDown(){
if(window.event.keyCode == _CMDRETURN)
{
if(event.srcElement.type != "submit" & event.srcElement.type != "textarea" & event.srcElement.type != "button")
{
window.event.keyCode = _CMDTAB;
}
else
{
window.event.keyCode =_CMDRETURN;
}
}
}
//-->
</script>