การเลือก หรือ ไม่เลือก checkbox ทั้งหมด และ hilight ด้วย jQuery อย่างง่าย

28 June 2010

ตัวอย่าง

Title
Data 1
Data 2
Data 3
Data 4
Data 5
Data 6
Data 7
Data 8
Data 9
Data 10
CSS Code ตัวอย่าง
 

<style type="text/css">
html,body{
	margin:0;
	padding:0;
	font-family:Tahoma, Geneva, sans-serif;
	font-size:12px;	
}
/* css สำหรับแถวคี่ */
.RowOdd{
	background-color:#EAEAEA;		
}
/* css สำหรับแถวคู่ */
.RowEven{
	background-color:#FBFBFB;	
}
</style>


HTML Code ตัวอย่าง
 

<table class="myTable" width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="49" align="center" bgcolor="#CCCCCC"><input name="checkAll" type="checkbox" id="checkAll" value="1" /></td>
    <td width="451" bgcolor="#CCCCCC">Title</td>
  </tr>
    <tr class="RowOdd">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="1" />
	</td>
    <td>Data 1</td>
  </tr>
    <tr class="RowEven">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="2" />
	</td>
    <td>Data 2</td>
  </tr>
    <tr class="RowOdd">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="3" />
	</td>
    <td>Data 3</td>
  </tr>
    <tr class="RowEven">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="4" />
	</td>
    <td>Data 4</td>
  </tr>
    <tr class="RowOdd">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="5" />
	</td>
    <td>Data 5</td>
  </tr>
    <tr class="RowEven">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="6" />
	</td>
    <td>Data 6</td>
  </tr>
    <tr class="RowOdd">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="7" />
	</td>
    <td>Data 7</td>
  </tr>
    <tr class="RowEven">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="8" />
	</td>
    <td>Data 8</td>
  </tr>
    <tr class="RowOdd">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="9" />
	</td>
    <td>Data 9</td>
  </tr>
    <tr class="RowEven">
    <td align="center">
      <input class="myChkBox"  name="Item[]" type="checkbox" id="Item[]" value="10" />
	</td>
    <td>Data 10</td>
  </tr>
    
</table>


Javascript Code ตัวอย่าง
 

<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(function(){
	var HilightCSS={
		"background-color":"#FFC"
	}
	var NoHilightCSS={
		"background-color":""
	}	
	$("#checkAll").click(function(){
		var parentTR=$(".RowOdd,.RowEven");
        var statusCheckBox=($(this).attr("checked")=="checked")?true:false;
		$(this).parents(".myTable").find(".myChkBox")
		.attr("checked",statusCheckBox);
		if($(this).attr("checked")=="checked"){
			parentTR.css(HilightCSS);
		}else{
			parentTR.css(NoHilightCSS);
		}
	});
	
	$("input.myChkBox").click(function(){
		var parentTR=$(this).parents(".RowOdd,.RowEven");
		if($(this).attr("checked")=="checked"){
			parentTR.css(HilightCSS);
		}else{
			parentTR.css(NoHilightCSS);
		}
	});
	
});
</script>

 



ตัวอย่างนี้ใช้งานกับ jQuery เวอร์ชั่น 1.7.1 เป็นต้นไป อาจจะต้องประยุกต์เพิ่มเติมหากใช้กับเวอร์ชั่นเก่าๆ





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

01 Feb 10 การใช้งาน method delay() ใน jQuery 1.4 อ่าน 2719 29 Oct 10 แนะนำ jQuery CountDown plugin สวยๆ สำหรับประยุกต์ใช้งาน อ่าน 2711 25 Sep 08 การกำหนด selectors ด้วย jQuery แบบตัวกรองพื้นฐาน อ่าน 2700 19 Oct 10 เริ่มต้นการใช้งาน jwplayer jquery plugin แสดง video บนเว็บไซต์ อ่าน 2683 26 Sep 10 การแสดงข้อมูล แนะนำ เส้นทาง ใน google map อ่าน 2617 29 Nov 09 กำหนด hilight ของ cell ข้อมูลในตาราง ด้วย jQuery และ CSS อย่างง่าย อ่าน 2608 25 Sep 08 หาตำแหน่งของ element แนวแกน x แกน y ด้วยฟังก์ชันของ jQuery อ่าน 2571 22 Aug 10 สร้าง waiting page ให้รอสักครู่ ก่อนลิ้งค์ ไปเว็บอื่น ด้วย jQuery และ php อ่าน 2552 06 Nov 10 แนะนำ jQuery UI Effect แบบ ไม่ต้องใช้งานร่วมกับ event show hide และ toggle อ่าน 2549 14 Mar 09 ค้นหาข้อความในหน้าเว็บเพจอย่างง่ายด้วย jQuery อ่าน 2541 12 Aug 10 ประยุกต์ ใช้ jQuery สร้างข้อความเลื่อน ซ้าย ขวา คล้าย marquee อ่าน 2517 02 Apr 11 การอ้างอิง object ในหน้าหลัก จากหน้า popup ด้วย jQuery อย่างง่าย อ่าน 2514 27 Nov 09 แก้ปัญหาความกว้าง option ของ select tag ใน ie ด้วย jQuery และ css อย่างง่าย อ่าน 2513 20 Oct 10 สร้าง video playlist ของ jwplayer jquery plugin ด้วย xml และการใช้งาน อ่าน 2502 08 Dec 09 แปลง jQuery object เป็น DOM object อ่าน 2467

บทความคนเข้าอ่านวันนี้

13 Feb 09 แปลงข้อความเป็นตัวเล็ก toLowerCase ตัวใหญ่ toUpperCase ด้วย javascript อ่าน 1877 18 Oct 08 การประยุกต์ใช้ฟังก์ขัน after() กับ text input อ่าน 4551 13 Oct 11 แนะนำการใช้งาน การเชื่อมต่อ facebook ด้วย php sdk v.3.1.1 อ่าน 672 25 Sep 08 เพิ่มข้อความหน้าหรือหลังตัวเลขหรือข้อความด้วยฟังก์ชัน str_pad อ่าน 1346 19 Oct 08 คำสั่ง SQL COUNT อ่าน 6496 30 Oct 10 การเลื่อน jQuery UI tab อัตโนมัติ ตามเวลาที่กำหนด อ่าน 5083 25 Sep 08 ใช้ PHP สร้าง Javascript อย่างง่าย อ่าน 4163 30 Mar 09 ค้นหาและ hilight ข้อความในหน้าเว็บเพจด้วย jQuery อ่าน 2765 22 Aug 10 สร้าง waiting page ให้รอสักครู่ ก่อนลิ้งค์ ไปเว็บอื่น ด้วย jQuery และ php อ่าน 2552 25 Sep 08 รวมส่วนประกอบต่างข้างบนเป็น CSS อ่าน 3603 25 Sep 08 เอา scrollbar ออกจาก textarea ด้วย CSS อ่าน 4055 06 Nov 09 สร้าง swap แบนเนอร์ effect แบบ fade ด้วย jQuery อย่างง่าย อ่าน 4639 22 Apr 10 ประยุกต์ tooltip มากกว่า 1 ตัวใน ฟอร์ม ด้วย jQuery อ่าน 5059 25 Sep 08 รู้จักฟังก์ชันของ jQuery ในการกำหนด Style Sheet อ่าน 3462 09 Sep 10 Google map API v.3 กับ jQuery ลากจุดหา พิกัด ค่า latitude และ longitude อ่าน 6372 25 Sep 08 ป้องกันคนคลิกขวา และ ป้องกันคน select ข้อความ อ่าน 1885 09 Jul 10 เพิ่มความเร็ว ให้กับการ cache ด้วย jquery ajax และ php cache class อ่าน 2265 19 Jun 10 สร้าง comment ด้วย social plugins ใน facebook api อย่างง่ายดาย อ่าน 17118 25 Sep 08 การกำหนด selectors ด้วย jQuery แบบตัวกรองพื้นฐาน อ่าน 2700 19 Feb 10 สร้าง Horizontal Accordion แนวนอน ด้วย jQuery อย่างง่าย อ่าน 3449
จำนวนผู้เยี่ยมชม 777890
คน 2012 © Copyright ninenik.com. All rights reserved.