获取输入值并显示在另一个div中 [英] Take input value and display in another div

查看:355
本文介绍了获取输入值并显示在另一个div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么以下不起作用。我从W3school的示例此处



基本上我想从输入文本中取值,当它改变并修改另一个div以包含该值时。我只想让div显示新的价值,但我确实希望它每次都改变它,所以我认为onchange是要走的路。

 <!DOCTYPE html> 
< html>
< head>
< script>
函数myFunction()
{
var div = document.getElementById('divID');
div.innerHTML = div.innerHTML + x.value;
}
< / script>
< / head>
< body>

输入您的姓名:< input type =textid =fnameonchange =myFunction()>

当您离开输入栏时,会触发一个功能,将输入文本转换为大写。< / p>

< div id =divID>< / div>

< / body>
< / html>

预先感谢此次的所有帮助。

你有两个问题,第一个是x未定义。
秒,你应该使用另一个触发器,以便每次都发生这种情况。



尝试一下:

  function myFunction()
{
var input = document.getElementById('fname')
var div = document.getElementById('divID' );
div.innerHTML = div.innerHTML + input.value;
}

并将您的html更改为:

 < input type =textid =fnameonkeypress =myFunction()> 


I cant for the life of me figure out why the following is not working. I took if from the W3school example here.

Basically I want to take the value from the input text when it changes and modify another div to include the value. I only want the div to show the new value, but I do want it to change it each time so I figured the onchange was the way to go.

    <!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
var div = document.getElementById('divID');
div.innerHTML = div.innerHTML + x.value;
}
</script>
</head>
<body>

Enter your name: <input type="text" id="fname" onchange="myFunction()">
<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>

<div id="divID"></div>

</body>
</html>

Thanks in advance for all the help on this one.

解决方案

You have 2 problems, first is that x is undefined. second you should use another trigger for this for this to happen each time.

try this out:

    function myFunction()
    {
        var input = document.getElementById('fname')
        var div = document.getElementById('divID');
        div.innerHTML = div.innerHTML + input.value;
    }

and change your html to:

<input type="text" id="fname" onkeypress="myFunction()">

这篇关于获取输入值并显示在另一个div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆