การกำหนดให้ input text เลื่อนโฟกัส focus เองอัตโนมัติ ด้วย jQuery

Bookmark and Share

การกรอกเลขที่บัตรเครดิต

HTML Code

<form id="form1" name="form1" method="post" action="">
<!--กำหนดชื่อของ input text แล้วตามด้วย _ ตามด้วย ลำดับที่ เช่น _1 ตัวที่ 1 , _2 ตัวที่ 2
ตัวอย่าง ชื่อ cardNo  ก็กำหนดเป็น  cardNo_1, cardNo_2 ...cardNo_n 
ตาม n จำนวนที่ต้องการ จากตัวอย่างข้างล่างมี 4 ตัว จากนั้นกำหนด maxlength ให้กับ
input text นั้นๆ  โดย maxlength จำนวนตัวอักษรที่จะกรอกได้ เช่น maxlength="4" 
หมายถึง สามารถกรอก ตัวอักษรได้ ไม่เกิน 4 ตัว-->
เลขบัตรเครดิต
  <input name="cardNo_1" type="text" id="cardNo_1" size="3" maxlength="4" />
  <input name="cardNo_2" type="text" id="cardNo_2" size="3" maxlength="4" />
  <input name="cardNo_3" type="text" id="cardNo_3" size="3" maxlength="4" />
  <input name="cardNo_4" type="text" id="cardNo_4" size="3" maxlength="4" />
</form> 

Javascript Code

<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript"> 
$(function(){
/*	สามารถเปลี่ยนจาก cardNo_ เป็นค่าที่ต้องการ  */
	$("input[name^='cardNo_']").keyup(function(){
		if($(this).val().length==$(this).attr("maxLength")){
			$(this).next("input").focus();
		}
	});	
});
</script>

ตัวอย่าง การกรอกเลขที่บัตรเครดิต

เลขบัตรเครดิต

การกรอกเลขที่บัตรประชาชน

HTML Code

  <form id="form1" name="form1" method="post" action="">
เลขที่บัตรประชาชน
  <input name="citizen_1" type="text" id="citizen_1" size="1" maxlength="1" style="width:10px;" /> 
  <input name="citizen_2" type="text" id="citizen_2" size="3" maxlength="4" style="width:35px;" /> 
  <input name="citizen_4" type="text" id="citizen_4" size="4" maxlength="5" style="width:40px;" /> 
  <input name="citizen_5" type="text" id="citizen_5" size="1" maxlength="1" style="width:10px;" /> 
  <input name="citizen_6" type="text" id="citizen_6" size="1" maxlength="2" style="width:15px;" />
</form> 

Javascript Code

<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript"> 
$(function(){
/*	สามารถเปลี่ยนจาก citizen_ เป็นค่าที่ต้องการ  */
	$("input[name^='citizen_']").keyup(function(){
		if($(this).val().length==$(this).attr("maxLength")){
			$(this).next("input").focus();
		}
	});	
});
</script>

ตัวอย่าง การกรอกเลขที่บัตรประชาชน

เลขที่บัตรประชาชน

การกรอกเบอร์โทรศัพท์

HTML Code

  <form id="form1" name="form1" method="post" action="">
  เบอร์โทรศัพท์
  <input name="telNo_1" type="text" id="telNo_1" size="1" maxlength="2" style="width:15px;" />
  - 
  <input name="telNo_2" type="text" id="telNo_2" size="3" maxlength="4" style="width:35px;" /> 
  -
  <input name="telNo_3" type="text" id="telNo_3" size="3" maxlength="4" style="width:35px;" />   
</form> 

Javascript Code

<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript"> 
$(function(){
/*	สามารถเปลี่ยนจาก telNo_ เป็นค่าที่ต้องการ  */
	$("input[name^='telNo_']").keyup(function(){
		if($(this).val().length==$(this).attr("maxLength")){
			$(this).next("input").focus();
		}
	});	
});
</script>

ตัวอย่าง การกรอกเบอร์โทรศัพท์

เบอร์โทรศัพท์ - -

การกรอกวันที่

HTML Code

  <form id="form1" name="form1" method="post" action="">
   วันที่ 
  <input name="date_1" type="text" id="date_1" size="1" maxlength="2" style="width:15px;" />
  /    
  <input name="date_2" type="text" id="date_2" size="1" maxlength="2" style="width:15px;" />
  / 
  <input name="date_3" type="text" id="date_3" size="3" maxlength="4" style="width:35px;" /> 
</form> 

Javascript Code

<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript"> 
$(function(){
/*	สามารถเปลี่ยนจาก date_ เป็นค่าที่ต้องการ  */
	$("input[name^='date_']").keyup(function(){
		if($(this).val().length==$(this).attr("maxLength")){
			$(this).next("input").focus();
		}
	});	
});
</script>

ตัวอย่าง การกรอกวันที่

วันที่ / /



บทความในหมวดที่่น่าสนใจ อื่นๆ jQuery Learning

04 Nov 08 การจำกัดจำนวนตัวอักษร ใน textarea ด้วย jquery อ่าน 2376 22 Nov 08 ซ่อนและแสดงเนื้อหาแบบหีบเพลง (accordion) ด้วย jquery อย่างง่าย อ่าน 2355 15 Feb 09 สร้างเมนู Accordion อย่างง่ายแบบที่ 2 ด้วย jQuery อ่าน 2313 16 Aug 09 jQuery UI คือ อะไร อ่าน 2300 19 Feb 09 สร้างกล่อง alert ข้อความเฉพาะตัว ด้วย jQuery และ CSS อย่างง่าย อ่าน 2258 25 Sep 08 jquery สคริปแรกของคุณ อ่าน 2248 25 Sep 08 ตัวอย่างการใช้งานฟังก์ชันของ jQuery ในการเรียกใช้ Class ใน CSS อ่าน 2186 02 Apr 09 สร้างรายการตัวเลือกให้กับ input text ด้วย jQuery อย่างง่าย อ่าน 2156 06 Nov 09 สร้าง swap แบนเนอร์ effect แบบ fade ด้วย jQuery อย่างง่าย อ่าน 2150 25 Sep 08 การกำหนด Selectors ด้วย jquery แบบ Basic อ่าน 2147 20 Dec 09 ประยุกต์การทำ overlay เพจ กับกล่องข้อความ ด้วย jQuery อ่าน 2079 04 Feb 09 แก้ไขข้อความแบบทันที ด้วย jQuery อ่าน 1981 25 Sep 08 รู้จักฟังก์ชันของ jQuery ในการเรียกใช้ Attribute อ่าน 1977 25 Sep 08 การกำหนด selectors ด้วย jQuery แบบลำดับขั้น ตอนที่ 1 อ่าน 1964 25 Sep 08 Event ของ jquery แบบ Basic อ่าน 1956
จำนวนผู้เยี่ยมชม 157924 คน 2010 © Copyright ninenik.com. All rights reserved.