04 มิถุนายน 2557

โค้ดสำหรับส่งค่า Tab เมื่อกดปุ่ม Enter (HTML+AJAX)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> Next Tab DEMO</title>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).on("keypress", ".TabOnEnter" , function(e)
{
//Only do something when the user presses enter
if( e.keyCode == 13 )
{
var nextElement = $('input[tabindex="' + (this.tabIndex+1) + '"]');

if(nextElement.length )
nextElement.focus();
else
$('input[tabindex="1"]').focus();

}
});
});//]]>
</script>
</head>
<body>
AAA:<input id="AAA" class='TabOnEnter' tabindex="1" autofocus/><br>
BBB:<input id="BBB" class='TabOnEnter' tabindex="2" /><br>
CCC:<input id="CCC" class='TabOnEnter' tabindex="3" /><br>
DDD:<input id="DDD" class='TabOnEnter' tabindex="4" /><br>
</body>
</html>

เครดิต: http://jsfiddle.net/konijn_gmail_com/WvHKA/

ไม่มีความคิดเห็น: