如何在 JavaScript 中自动填充与另一个字段相同的字段?
javascriptweb developmentobject oriented programming
以下是在 JavaScript 中自动填充与另一个字段相同的字段的代码 −
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } input { padding: 5px; margin: 5px; display: block; } button { padding: 3px; } </style> </head> <body> <h1>Autofill a filled textbox same as another</h1> <input type="text" class="addr" placeholder="address" /> <button class="Btn">Same as above</button> <input type="text" class="addr" placeholder="address" /> <script> let BtnEle = document.querySelector(".Btn"); let addrEle = document.querySelectorAll(".addr"); BtnEle.addEventListener("click", () => { addrEle[1].value = addrEle[0].value; }); </script> </body> </html>
输出
在第一个文本框中输入地址 −
单击‘;Same as above’按钮 −