Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中将文本输出到输入字段_Javascript_Html - Fatal编程技术网

在JavaScript中将文本输出到输入字段

在JavaScript中将文本输出到输入字段,javascript,html,Javascript,Html,试图按下在输入字段中输出文本的按钮。功能是在按下时将文本“x”输出到输入字段,该字段设置为只读。只允许使用js和html。以下是迄今为止我在HTML和js中获得的信息: <button id="button4" onclick="output()">Hokus Pokus</button> <input id="printoutput" readonly="true" type="text"> 为什么不起作用?您需要设置输入的值,而不是内部html d

试图按下在输入字段中输出文本的按钮。功能是在按下时将文本“x”输出到输入字段,该字段设置为只读。只允许使用js和html。以下是迄今为止我在HTML和js中获得的信息:

 <button id="button4" onclick="output()">Hokus Pokus</button>
 <input id="printoutput" readonly="true" type="text">

为什么不起作用?

您需要设置输入的值,而不是内部html

document.getElementById(“打印输出”).value=“x”
Hokus Pokus

像这样做,它就像一个符咒:

函数输出(){
document.getElementById(“打印输出”).value=“x”;
}
Hokus Pokus
已修复。他这样做:

function output() {

document.getElementById("printoutput").innerHTML = "x";

}
何时应该:

function output() {

document.getElementById("printoutput").value = "x";

}

哦,天哪,这显然是我的疏忽。谢谢
function output() {

document.getElementById("printoutput").value = "x";

}