ASP.NET jQuery 實例2 (表單中使用回車在TextBox之間向下移動)
更新時間:2012年01月13日 22:20:10 作者:
每次當用戶在一個文本框輸入完數(shù)據(jù)后,更希望在敲入回車鍵后,焦點會自動移動到下一個文本框
通過下面的代碼可以實現(xiàn)這種切換的效果。
首先我們來看界面:
界面代碼:
<body>
<form id="form1" runat="server">
<div align="center">
<fieldset style="width: 400px; height: 200px;">
<table cellpadding="3" cellspacing="3" border="0">
<tr>
<td>
<asp:Label ID="lblName" Text="姓名: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblAddress" Text="地址: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtAddress" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblContact" Text="聯(lián)系電話: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtContact" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblEmail" Text="電子郵箱: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="提交" runat="server" />
<asp:Button ID="btnReset" Text="重置" runat="server" />
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
腳本代碼:
<head runat="server">
<title>Recipe2</title>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("input:text:first").focus(); // TextBox轉(zhuǎn)換成html控件為<input type="text"/>
$("input:text").bind("keydown", function (e) {
if (e.which == 13) { // 獲取Enter鍵值
e.preventDefault(); // 阻止表單按Enter鍵默認行為,防止按回車鍵提交表單
var nextIndex = $("input:text").index(this) + 1;
$("input:text")[nextIndex].focus();
}
});
$("#<%=btnReset.ClientID%>").click(function () {
$("form")[0].reset();
});
});
</script>
</head>
首先我們來看界面:

界面代碼:
復制代碼 代碼如下:
<body>
<form id="form1" runat="server">
<div align="center">
<fieldset style="width: 400px; height: 200px;">
<table cellpadding="3" cellspacing="3" border="0">
<tr>
<td>
<asp:Label ID="lblName" Text="姓名: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblAddress" Text="地址: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtAddress" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblContact" Text="聯(lián)系電話: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtContact" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblEmail" Text="電子郵箱: " runat="server"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmail" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="提交" runat="server" />
<asp:Button ID="btnReset" Text="重置" runat="server" />
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
腳本代碼:
復制代碼 代碼如下:
<head runat="server">
<title>Recipe2</title>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("input:text:first").focus(); // TextBox轉(zhuǎn)換成html控件為<input type="text"/>
$("input:text").bind("keydown", function (e) {
if (e.which == 13) { // 獲取Enter鍵值
e.preventDefault(); // 阻止表單按Enter鍵默認行為,防止按回車鍵提交表單
var nextIndex = $("input:text").index(this) + 1;
$("input:text")[nextIndex].focus();
}
});
$("#<%=btnReset.ClientID%>").click(function () {
$("form")[0].reset();
});
});
</script>
</head>
您可能感興趣的文章:
- asp.net ASPxTextBox等控件實現(xiàn)"回車模擬Tab"的 常用代碼整理
- asp.net中綁定TextBox回車事件的解決方法
- ASP.net Textbox的技巧使用
- ASP.NET jQuery 實例1(在TextBox里面創(chuàng)建一個默認提示)
- ASP.NET jQuery 實例3 (在TextBox里面阻止復制、剪切和粘貼事件)
- ASP.NET jQuery 實例4(復制TextBox的文本到本地剪貼板上)
- Asp.net TextBox的TextChanged事件使用介紹
- Asp.net之TextBox只允許輸入數(shù)字的方法總結
- asp.net中TextBox只能輸入數(shù)字的最簡潔的兩種方法
- ASP.NET中為TextBox中添加calendar.js示例代碼
- ASP.NET中 TextBox 文本輸入框控件的使用方法
相關文章
JQuery Ajax執(zhí)行跨域請求數(shù)據(jù)的解決方案
今天小編就為大家分享一篇關于JQuery Ajax執(zhí)行跨域請求數(shù)據(jù)的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12比較簡單的jquery教程 Easy Ajax with jQuery 中文版全集
比較簡單的jquery教程 Easy Ajax with jQuery 中文版全集...2007-11-11jquery插件jquery.LightBox.js實現(xiàn)點擊放大圖片并左右點擊切換效果(附demo源碼下載)
這篇文章主要介紹了jquery插件jquery.LightBox.js實現(xiàn)點擊放大圖片并左右點擊切換效果,可實現(xiàn)仿相冊插件切換效果,并附代碼demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02jquery 獲取select數(shù)組與name數(shù)組長度的實現(xiàn)代碼
下面小編就為大家?guī)硪黄猨query 獲取select數(shù)組與name數(shù)組長度的實現(xiàn)代碼。小編覺得挺不錯的, 現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06Jquery 動態(tài)添加元素并添加點擊事件實現(xiàn)過程解析
這篇文章主要介紹了Jquery 動態(tài)添加元素并添加點擊事件實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10jQuery 移動端拖拽(模塊化開發(fā),觸摸事件,webpack)
這篇文章主要介紹了jQuery 移動端拖拽(模塊化開發(fā),觸摸事件,webpack)的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10