Javascript กำหนดค่า readonly ให้กับ input

การกำหนดค่า readonly ให้กับ input มีหลายวิธีครับ
<input type=text readonly>

วิธีด้านบนเป็นการกำหนดอย่างง่ายๆด้วย HTML ซึ่งจะมีผลให้เราไม่สามารถแก้ไขค่าใน input ได้ แต่ input จะถูก submit ได้ตามปกติ ซึ่งแตกต่างจากการ disabled ครับ เนื่องจากการ disabled input จะไม่ถูก submit ไปด้วย
/* กำหนด input เป็น readonly ด้วย javascript */
document.getElementById('inputId').setAttribute('readonly', true);
/* ยกเลิก readonly */
document.getElementById('inputId').removeAttribute('readonly');

โค้ดด้านบน เป็นการกำหนดค่า readonly ด้วย Javascript ครับ ซึ่งต้องกำหนดด้วย setAttribute ครับ
/* กำหนด input เป็น readonly ด้วย javascript */
$G('inputId').set('readonly', true);
/* ยกเลิก readonly */
$G('inputId').set('readonly', false);

ถ้าคุณใช้ GAjax ให้คุณใช้โค้ดด้านบน
ผู้เขียน goragod โพสต์เมื่อ 18 มี.ค. 2556 เปิดดู 16,035 ป้ายกำกับ Javascript
^