jQuery
เพิ่มแถวเข้าไปในตารางด้วย jQuery
วัชรเมธน์ ศรีเนธิโรทัย
06/08/2552
มันเป็นอะไรที่ง่ายมากๆครับผม ถ้าเกิดว่าเราเขียนด้วย javascript ธรรมดา คงต้องใช้โค้ดหลายบรรทัดทีเดียว แต่ถ้าใช้ jQuery นะแค่ 2-3 บรรทัดเอง จ้า มาดูกันว่าทำยังไง ภาพสำเร็จหน้าตาอย่างนี้

Live Demo
โครงสร้าง HTML :
เตรียมปุ่มไว้ด้วย พอคลิกปุ่ม ให้เพิ่มแถวเข้าไป
<input type="button" name="button" id="button" value="เพิ่มแถว" />
<table id="table">
<tr>
<th>ลำดับ</th>
<th>สาขา</th>
</tr>
<tr>
<td>1</td>
<td>สุรวงศ์</td>
</tr>
<tr>
<td>2</td>
<td>เพชรบุรีตัดใหม่</td>
</tr>
</table> |
<input type="button" name="button" id="button" value="เพิ่มแถว" />
<table id="table">
<tr>
<th>ลำดับ</th>
<th>สาขา</th>
</tr>
<tr>
<td>1</td>
<td>สุรวงศ์</td>
</tr>
<tr>
<td>2</td>
<td>เพชรบุรีตัดใหม่</td>
</tr>
</table>
include jQuery เข้ามา :
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
เขียนโค้ด :
$(document).ready(function(){
$('#button').click(function(){
var tr = $('<tr><td>3</td><td>รามอินทราทีมA</td></tr>');
$('#table').append(tr);
});
}) |
$(document).ready(function(){
$('#button').click(function(){
var tr = $('<tr><td>3</td><td>รามอินทราทีมA</td></tr>');
$('#table').append(tr);
});
})
โค้ดทั้งหมด :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>Add row to table</title>
<script type="text/javascript">
$(document).ready(function(){
$('#button').click(function(){
var tr = $('<tr><td>3</td><td>รามอินทราทีมA</td></tr>');
$('#table').append(tr);
});
})
</script>
</head>
<body>
<input type="button" name="button" id="button" value="เพิ่มแถว" />
<table id="table">
<tr>
<th>ลำดับ</th>
<th>สาขา</th>
</tr>
<tr>
<td>1</td>
<td>สุรวงศ์</td>
</tr>
<tr>
<td>2</td>
<td>เพชรบุรีตัดใหม่</td>
</tr>
</table>
</body>
</html> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>Add row to table</title>
<script type="text/javascript">
$(document).ready(function(){
$('#button').click(function(){
var tr = $('<tr><td>3</td><td>รามอินทราทีมA</td></tr>');
$('#table').append(tr);
});
})
</script>
</head>
<body>
<input type="button" name="button" id="button" value="เพิ่มแถว" />
<table id="table">
<tr>
<th>ลำดับ</th>
<th>สาขา</th>
</tr>
<tr>
<td>1</td>
<td>สุรวงศ์</td>
</tr>
<tr>
<td>2</td>
<td>เพชรบุรีตัดใหม่</td>
</tr>
</table>
</body>
</html>
มันง่ายใช่ไหม
Like this:
Like Loading...
วัชรเมธน์ ชิษณุคุปต์ ศรีเนธิโรทัย
Programmer, Blogger
ชีวิตมันคือการเดินทางเพื่อค้นหาอะไรบางอย่าง และเพื่อค้นพบอะไรบางอย่าง สิ่งที่รู้แล้วจะได้บอกกล่าวให้คนข้างหลังได้ฟัง เพื่อเดินลุยเข้าไปในพงหนามอย่างเตรียมใจไว้แล้วว่าอาจจะต้องเจ็บ
sitti says:
27/08/2552 at 27/08/2552
ขอบคุณครับ