คุณสมบัติ HTML DOM input number readOnly จะคืนค่าและแก้ไขว่าฟิลด์อินพุตเป็นแบบอ่านอย่างเดียวหรือไม่ในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
-
กลับมาอ่านอย่างเดียว
object.readOnly
-
กำลังแก้ไขอ่านอย่างเดียว
object.readOnly = true | false
ตัวอย่าง
ให้เราดูตัวอย่างของการป้อนข้อมูลคุณสมบัติอ่านอย่างเดียว -
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
height:100%;
}
p{
font-weight:700;
font-size:1.1rem;
}
input{
display:block;
width:35%;
border:2px solid #fff;
background-color:transparent;
color:#fff;
font-weight:bold;
padding:8px;
margin:1rem auto;
outline:none;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
</style>
</head>
<body>
<h1>DOM Input number readOnly property Demo</h1>
<p>Hi, Select your day of birth?</p>
<input type="number" class="numberInput">
<button onclick="rFunction()" class="btn">Read Only</button>
<button onclick="rWFunction()" class="btn">Read & Write</button>
<script>
function rFunction() {
var monthInput = document.querySelector(".numberInput");
monthInput.readOnly =true;
}
function rWFunction(){
var monthInput = document.querySelector(".numberInput");
monthInput.readOnly =false;
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

คลิกที่ “อ่านอย่างเดียว ” แล้วต่อ “อ่านและเขียน ” เพื่อทำความเข้าใจวิธีการทำงานของคุณสมบัติอ่านอย่างเดียว

