ในกรณีที่เราต้องการ remove attribute ออกจาก element ภายในเพจ jQuery ได้เตรียมคำสั่งไว้คำสั่งหนึ่งคือ removeAttr อย่างถ้าสมมติว่า ในฟอร์มของเรามีช่องเท็กบ้อกช่องหนึ่ง ซึ่งเราเซตไว้ว่าให้อ่านได้อย่างเดียว
<input type="text" name="username" id="username" readonly="readonly" /> |
แล้วเราต้องการให้ยูสเซ่อร์สามารถป้อนข้อมูลเข้าไปในช่องเท็กบ้อกนี้ได้ เราก็ใช้คำสั่ง
$('#username').removeAttr( 'readonly' ); |
ช่องเท็กบ้อกนี้ก็จะป้อนข้อมูลได้ทันที ถ้าสังเกตให้ดี ผลลัพธ์ที่ได้นั้น จะเหมือนกับการเซต readonly ให้เป็น false นั่นเอง
<!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-removeAttr-command</title> <script type="text/javascript"> $(document).ready(function(){ $('#remove').click(function(){ $('#username').removeAttr( 'readonly' ); }); }) </script> </head> <body> <input type="text" name="username" id="username" readonly="readonly" /> <input name="remove" type="button" id="remove" value="Remove Attr" /> </body> </html> |
Leave a Reply