当我在 textbox1 中写一些东西时,我也想在 textbox 2 中显示相同的文本

问题描述

我制作了两个文本框,我将给出文本框 1 和文本框 2 的 ID,这是代码 - <input type="text" id="textBox1"> <input type="text" id="textBox2"> 问题是当我在 textBox1 中写一些东西时,我也想在 textBox 2 中显示

解决方法

试试这个:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="text" id="textbox1" onkeyup="previewText(this.value)" placeholder="">
<br />
<input type="text" id="textbox2" />
<script>
    function previewText(name) {
        document.getElementById("textbox2").value = name;
    }
</script>
</body>
</html>
,
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" name="input1" id="input1" onkeypress="myfunction(event)">
    <input type="text" name="input2" id="input2" value="">
    <script>
        function myfunction(t) {
            var i=document.getElementById("input2")
            if(t.key=="Enter"){
                i.value=document.getElementById("input1").value
            }
            else{
                i.value=document.getElementById("input1").value+t.key;
            }
            
        }
        </script>
</body>
</html>