繁体   English   中英

我无法使用 JavaScript 访问我的 canvas 上的输入值

[英]I am not able to access input value on my canvas using JavaScript

I am not able to get the value of input statement in my JavaScript function, as i want my output on my canvas section of html but it is not showing anything on my canvas as well as in console.log function also. 请帮助解决这个问题。 谢谢:)

 var x = document.getElementById("name").value; console.log(x); function myfunction() { var canvas = document.getElementById('page'); var ctx = canvas.getContext("2d"); ctx.font = "15px Arial"; ctx.fillText(x, 10, 50); }
 <:DOCTYPE html> <html> <head> <title>yoyo</title> </head> <body> <p>hi everyone;</p> <div> <label>Your text here-</label><br> <input type="text" name="name" id="name"> </div> <div> <canvas id="page" width="200" height="300" style="border: solid; background: yellow;"></canvas> <button onclick="myfunction()">click</button> </div> </body> </html>

下面的代码将使您的输入记录在控制台中。 您需要在 myFunction function 中包含控制台日志,这样只有在用户单击“单击”按钮后,输入的值才会打印到控制台/检查。 你不应该创建一个需要用户输入的变量,并立即尝试访问它的值,因为当程序加载时,用户还没有在输入中输入任何值。 所以下面,你的 x 变量是通过访问#id 创建的,用户填写输入字段,单击按钮,然后检查 x 变量的值,并打印到控制台。

var x = document.getElementById("name");

function myfunction() {
  console.log(x.value);
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM