简体   繁体   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. 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. please help to solve this problem.请帮助解决这个问题。 thanks:)谢谢:)

 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>

The code below will get your input logged in the console.下面的代码将使您的输入记录在控制台中。 You need to have the console log inside of your myFunction function, so that the value of the input is only printed to console/checked once the user clicks the 'click' button.您需要在 myFunction function 中包含控制台日志,这样只有在用户单击“单击”按钮后,输入的值才会打印到控制台/检查。 You should not create a variable requiring user input, and immediately try to access its value, because when the program is loaded the user has not input any value into the input yet.你不应该创建一个需要用户输入的变量,并立即尝试访问它的值,因为当程序加载时,用户还没有在输入中输入任何值。 So below, your x variable is created by accessing the #id, the user fills in the input field, clicks the button, then the value of the x variable is checked, and printed to the console.所以下面,你的 x 变量是通过访问#id 创建的,用户填写输入字段,单击按钮,然后检查 x 变量的值,并打印到控制台。

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

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

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

相关问题 我可以使用 JavaScript function 反转字符串,但是如何使用表单输入和按钮单击 Angular 来反转字符串? - I am able to reverse a string using JavaScript function but how to do it using a form input and button click in Angular? 我无法在我的网站上访问我的问题的正确ID - I am not able to access correct id of my ques in my website 我使用Pixastic的HTML5 Canvas和Javascript过滤器处理图像时遇到问题,我在做什么错? - I am having trouble processing my image using HTML5 Canvas and Javascript Filters from Pixastic, what am I doing wrong? 我可以画盒子,但不能将其删除! (帆布) - I am able to draw the box but I am not able to remove it! (Canvas) Javascript,我无法将 cursor 输入框 - Javascript, I am not being able to cursor to input box 为什么我不能在表中添加输入文本? - why am I not able to add input text into my table? 我无法将输入值设置为“筹码” - I am not able to style my input values as 'chips' 为什么我不能使用JavaScript访问CSS属性? - Why am I not able to access CSS properties with JavaScript? 我无法在输入类型的文本框中输入数值 - I am not able to enter numeric value in text box of type input 为什么我无法在setTimeout中访问事件值? - Why I am not able to access event value in setTimeout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM