简体   繁体   English

我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值?

[英]How do i use JavaScript to take the input from a text box to a variable then print the value of a variable?

I want to take the value of a text box and put it with a variable, then print the value of the variable as text on the page when a button is pressed.我想获取文本框的值并将其与变量放在一起,然后在按下按钮时将变量的值作为文本打印在页面上。 I'm using JavaScript and not using the form tag.我正在使用 JavaScript 而不是使用表单标签。 So, how do I do this with what I have?那么,我如何用我所拥有的来做到这一点?

my code:我的代码:

var x = document.getElementById("I1");
var y = document.getElementById("I2");
var z = x+y;

document.getElementById("B2").onclick = function myFunction2(){
  documen.getElementById("H2").innerHTML = x;
}

If I understand you correctly, you'll simply want to grab the .innerHTML from your <textarea> , and assign that to x .如果我理解正确,您只需从<textarea>获取.innerHTML ,并将其分配给x Also note that you have documen instead of document inside of your click function.另外请注意,你有documen ,而不是document的点击函数中。

Note that you're probably also looking to output z rather than x .请注意,您可能还希望输出z而不是x This will only work (the way you expect) if both x and y are integers, so it's also a good idea to run parseInt() over the two values.如果xy都是整数,这只会工作(您期望的方式),因此对两个值运行parseInt()也是一个好主意。

Here's a working example:这是一个工作示例:

 var x = parseInt(document.getElementById("I1").innerHTML); var y = parseInt(document.getElementById("I2").innerHTML); var z = x + y; document.getElementById("B2").onclick = function myFunction2() { document.getElementById("H2").innerHTML = z; }
 <textarea id="I1">1</textarea> <br /> <div id="I2">2</div> <br /> <button id="B2">Button</button> <br /> <br /> <div id="H2"></div>

Are I1 and I2 the ids of your textbox? I1I2是您的文本框的 ID 吗? because to get the inside text you need to use因为要获取您需要使用的内部文本

var x = document.getElementById("I1").value;

You should rely on the value attribute of the textarea instead of its innerHTML attribute, and fetch that in the click event handler of the button.您应该依赖 textarea 的value属性而不是其innerHTML属性,并在按钮的 click 事件处理程序中获取它。

 document.getElementById("B2").onclick = function() { var x = document.getElementById("I1").value; document.getElementById("H2").innerHTML = x; }
 <textarea id="I1">1</textarea> <br /> <button id="B2">Button</button> <br /> <br /> <div id="H2"></div>

 document.getElementById("B2").onclick = function myFunction2(){ var x = Number(document.getElementById("I1").value); var y = Number(document.getElementById("I2").value); var z = x+y; document.getElementById("H2").innerHTML = z; }
 <input type="text" id="I1" value="" /> + <input type="text" id="I2" value="" /> = <button id="B2"> ? </button> <span id="H2"> </span>

暂无
暂无

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

相关问题 如何使用 HTML 文本框值作为 JavaScript 参数或变量并调用 JavaScript Z86408593C34AF7261F 值框? - How do I use an HTML text box value as a JavaScript parameter or variable and call the JavaScript Function with the text box value? 如何使用HTML输入框设置JavaScript变量? - How do I use a HTML input box to set a JavaScript variable? 如何访问输入文本框中的值? 我不能在不使用 ref 的情况下直接从事件变量中获取值吗? - How do i access the value inside the input text box? cant i fetch the value directly from the event variable without using ref? javascript - 将文本输入框中的值传递给变量,以用于 Fetch API - javascript - pass a value from text input box to a variable, for use in Fetch API 如何获取日期输入的值并将其保存到全局变量? - How do I take the value of a date input and save it to a global variable? 如何从XMLHttpRequest解析javascript中的该值并将其用作变量? - How do I parse this value in javascript from XMLHttpRequest and use it as a variable? 需要从文本框中获取变量输入以创建与 USPS 跟踪系统一起使用的链接 - Need to take variable input from text box to create a link to use with USPS Tracking System 需要将用户输入从提示框(Javascript)放入String变量。 我该怎么做? - Need to place user input from a prompt box (Javascript) into a String variable. How do I do so? 如何使用jquery更改变量中输入框的值? - How do I change the value of an input box in a variable using jquery? 如何从一个javascript变量获取一个值,通过一个按钮用作一个php输入变量,没有或没有提交按钮? - how to take a Value from a javascript variable, to be used as a php input variable via a button, without or not a submit button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM