HTML,javascript - 显示标签的输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19050841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:10:47  来源:igfitidea点击:

HTML, javascript - Displaying input value to a label

javascriptjqueryhtmlinputlabels

提问by user2362791

I want to display the value of an <input>to a label or something like that, by using onkeyup so it's real time. But can only do it by using a second input instead of the label but I just want to display the value.

我想<input>通过使用 onkeyup将 an 的值显示到标签或类似的东西上,以便它是实时的。但只能通过使用第二个输入而不是标签来做到这一点,但我只想显示该值。

This is my Javascript function:

这是我的 Javascript 函数:

function copyTo(obj) {
    document.getElementById("utmatning").value = obj.value
}

And the HTML:

和 HTML:

<label>E-post:</label>
<input id="inmatning" type="text" onkeyup="copyTo(this)" placeholder="Skriv in e-post…">
<span>V?r ?terkoppling kommer att skickas till:</span>
<br>
<label id="utmatning"></label>

回答by Anton

use textContentfor label

使用textContent的标签

 document.getElementById("utmatning").textContent = obj.value

DEMO

演示

回答by Jurgen Vandw

You can also call the keyup function of jquery and update the label on every keystroke: http://api.jquery.com/keyup/

您还可以调用 jquery 的 keyup 函数并在每次击键时更新标签:http: //api.jquery.com/keyup/

回答by Ahsan Shah

try this

试试这个

$( "#inmatning" ).keyup(function() {
    $("#utmatning").text($("#inmatning").val());
});

Or

或者

$( "#inmatning" ).keyup(function() {
    $("#utmatning").html($("#inmatning").val());
});

both should work

两者都应该工作

回答by Anup

    Try this also if you are seeking short method use jquery :

    $( "#inmatning" ).keyup(function() {
        $("#utmatning").text($(this).val());
    });

    or use 
    $( "#inmatning" ).keyup(function() {
        $("#utmatning").text($("#inmatning").val());
    });

but you are using function then you can also use:

function copyTo(that){
 $("#utmatning").text($(that).val());
}