Js控制回车键

在ASP.NET网页中,使用服务器button按钮,默认第一个button响应回车键的点击事件。

<wbr></wbr>

比如

<wbr></wbr>

<wbr>&lt;body&gt;<br><wbr><wbr><wbr> &lt;form id="form1" runat="server"&gt;<br><wbr><wbr><wbr> &lt;div&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 姓名:&lt;asp:TextBox ID="txtName" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;br /&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 密码:&lt;asp:TextBox ID="txtPwd" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;br /&gt;<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>&lt;asp:Button ID="btnSubmit" runat="server" Text="登陆"</strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr>οnclick="btnSubmit_Click" /&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr> &lt;input id="Reset1" type="reset" value="重置" /&gt;<br><wbr><wbr><wbr> &lt;/div&gt;<br><wbr><wbr><wbr> &lt;/form&gt;<br> &lt;/body&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

这个表单中就按下回车就默认触发了btnSubmit_Click事件。

<wbr></wbr>

很多时候,第一个按钮这不是用户在点击回车后想做事情。一般来说,回车键是完成输入工作后提交数据如、关闭当前弹出窗口、输入关键字后回车进行搜索。

<wbr></wbr>

在ASP.NET网页中设置回车键的解决方法是使用javascript的onkeydown方法捕捉键盘点击事件,使用event.keyCode来获取用户点击的键位。

<wbr></wbr>

<head>
<wbr><wbr><wbr> &lt;title&gt;&lt;/title&gt;<br><wbr><wbr><wbr> &lt;link href="css/admin.css" type="text/css" rel="stylesheet"/&gt;<br><wbr><wbr><wbr> &lt;script type="text/javascript"&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> function <strong>KeyDown</strong>() {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> var btnsearch = document.getElementByIdx_x_x('<strong>&lt;%=btnSearch.ClientID %&gt;</strong>');</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>// 如果按下回车键</strong><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> if (window.event.keyCode == 13) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> btnsearch.click();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>//取消回车键的默认操作</strong><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> window.event.returnValue = false<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr> &lt;/script&gt;<br> &lt;/head&gt;<br> &lt;<strong>body οnkeydοwn="KeyDown()"&gt;</strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值