
เช็กบ้อกมี attribute ตัวหนึ่งที่จะเป็นตัวบอกว่า ตัวมันเองถูกติ๊กเลือกหรือเปล่า คือ checked ถ้าหากว่าผู้ใช้ติ๊กเครื่องหมายถูก checked จะมีค่าเท่ากับ true ถ้าไม่ถูกเช็กจะมีค่าเท่ากับ false jQuery นั้นมีคำสั่งดึงค่าค่าจาก attribute ของคอนโทรลตัวไหนๆ ก็ตามง่ายๆ ดูตัวอย่าง
<!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>jquery checkbox checked</title>
<script type="text/javascript">
$(document).ready(function(){
$('#button').click(function(){
alert( $('#checkbox') .attr( 'checked' ) );
});
})
</script>
</head>
<body>
<p>
<input type="button" name="button" id="button" value="ตรวจ" />
</p>
<p>
<input type="checkbox" name="checkbox" id="checkbox" /> ติ๊กถูก หรือ ติ๊กถูกออก แล้วกดปุ่มดู
</p>
</body>
</html> |
$(‘#checkbox’) .attr( ‘checked’ ) ถ้าเช็กบ้อกถูกติ๊ก คำสั่งนี้จะให้ค่าเ็ป็น true ถ้าไม่ จะเป็น false เห็นมั้ยครับว่าเราสามารถเช็กได้ง่ายๆ ท่านสามารถ get ค่าจาก attribute ได้ทุก attribute ครับ
$(‘#checkbox’) .attr( ‘value’ )
$(‘#checkbox’) .attr( ‘readonly’ )
ถ้าท่านอยากทราบว่า html แต่ละตัวมี attribute อะไรบ้าง ก็หาหนังสือ html มาอ่านสักเล่ม หรือหาในกูเกิลก็ได้จ่ะ





nightmare says:
09/08/2552 at 09/08/2552
โอวไม่ได้เข้ามาแป๊บเดียวมีอะไรใหม่ๆเพียบเลย แบบว่าไปอยู่ในที่ทุระกันดารไม่มีเน็ตให้ใช้มาอ่ะครับเหอๆแล้วก็ไม่มีตังจ่าย CDMA ด้วย 555