在尝试了我想要做的事情的多种方法,但都失败了,我在这里问。这可能是很基本的,但我就是做不到。
我本质上想做的是:
如果用户必须按一个按钮来使用变量的值来更新元素,那也可以,我只想这样做。
发布于 2017-12-27 21:18:06
好吧,我必须纠正自己。另一次尝试成功了,结果是“未定义”。
<head>
    <meta id="test3" charset="utf-8">
    <link rel="stylesheet" href=".\css\Starter.css">
    <title id="test1">TITEL</title>
    <script>
        function txtSet(txtInp) {
            var txt = txtInp.value
            document.getElementById('txtP').innerHTML = txt
            }
    </script>
</head>
<body>
    <input type="text" id="txtInp" onkeyup="txtSet(txtInp.value)"></input>
    <p id="txtP"></p>
</body>发布于 2017-12-27 20:49:22
我认为您正在搜索onkeyup,如果是这样的话:您可以如下所示:
在html中
<input type="text" name="name" id="id" onkeyup="yourFunction(this.value);">在你的js文件里
var theVariable;
function yourFunction(theTextInTheTextBox){
theVariable = theTextInTheTextBox;
}它也可以是onkeypress或onkeydown事件,只需尝试三个,看看哪一个是您真正要搜索的。为了了解这三者之间的区别,我建议您看看这个链接
发布于 2017-12-27 21:00:25
试试这个:
    <body>
    <script>
    var a = "";
    function changeVariable(){
         document.getElementById('demo2').value=document.getElementById('demo').value;
    }
    </script>
    <input type="text" id="demo" onkeyup="changeVariable()"></input>
    <input type="text" id="demo2"></input>
    </body>https://stackoverflow.com/questions/47998334
复制相似问题