Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

JavaScript Auto-filling one field same as other (กรอกข้อมูลซ้ำอัตโนมัติ)


สำหรับการกรอกข้อมูลในช่องหนึ่งให้เหมือนกับอีกช่องหนึ่งโดยอัตโนมัติ สคริป्ट JavaScript มีดังนี้ -

ตัวอย่าง

<!DOCTYPE html> <html> <head> <style>    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    } </style> </head> <body> <h1>JavaScript Auto-filling one field same as other</h1> <h2>Enter your primary address</h2> Address<input class="address" type="text" /> Pin Code<input class="pincode" type="text" /> <h4>Secondary Address same as primary</h4> <input class="check" type="checkbox" /> Same Address <h2>Enter your secondary address</h2> Address<input class="address" type="text" /> Pin Code<input class="pincode" type="text" /> <script type="text/javascript">    let checkEle = document.querySelector(".check");    let addressEle = document.querySelectorAll(".address");    let pinCodeEle = document.querySelectorAll(".pincode");    checkEle.addEventListener("change", (event) => {       if (event.target.checked) {          console.log(addressEle[0].value);          addressEle[1].value = addressEle[0].value;          pinCodeEle[1].value = pinCodeEle[0].value;       }    }); </script> </body> </html>

ผลลัพธ์

JavaScript Auto-filling one field same as other (กรอกข้อมูลซ้ำอัตโนมัติ)

เมื่อกรอกที่อยู่หลักและคลิก checkbox "Same Address" -

JavaScript Auto-filling one field same as other (กรอกข้อมูลซ้ำอัตโนมัติ)