คุณสมบัติ defaultValue ของเดือนที่ป้อน HTML ส่งคืนและแก้ไขค่าเริ่มต้นของฟิลด์อินพุตประเภท=”เดือน” ในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
1. คืนค่าเริ่มต้น
object.defaultValue
2. การแก้ไขค่าเริ่มต้น
object.defaultValue=value
ที่นี่ ค่า เป็นวันที่ในรูปแบบสตริง เช่น “2019-03”
ตัวอย่าง
ให้เราดูตัวอย่างของเดือนที่ป้อน HTML DOM คุณสมบัติ defaultValue -
<!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.2rem;
}
input{
width:35%;
border:2px solid #fff;
background-color:transparent;
color:#fff;
font-weight:bold;
padding:8px;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
.show{
font-size:1.5rem;
font-weight:bold;
}
</style>
</head>
<body>
<h1>DOM Input month defaultValue Example</h1>
<p>Hi, Enter your month of birth</p>
<input type="month" class="monthInput">
<button onclick="changeValue()" class="btn">Submit</button>
<div class="show"></div>
<script>
function changeValue() {
var monthInput = document.querySelector(".monthInput");
var showMsg = document.querySelector(".show");
monthInput.defaultValue = "1997-10";
monthInput.value= monthInput.defaultValue;
showMsg.innerHTML =":D hehe I changed your month of birth into mine!";
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะให้ผลลัพธ์ดังต่อไปนี้ -

ป้อนเดือนเกิดของคุณแล้วคลิก “ส่ง ปุ่ม ”


ที่นี่คุณสามารถสังเกตได้ว่าเดือนที่คุณเลือกถูกแทนที่ด้วยเดือนเริ่มต้นของฉัน “ตุลาคม-2540”