简体   繁体   English

如何从文本区域获取值并将其存储在变量中

[英]How to get a value from a textarea and store it in a variable

I need to type something inside a textarea, then take the text that was written there and store it in a variable.我需要在 textarea 中输入一些内容,然后将在那里写入的文本存储在一个变量中。

Example:例子:

I type "Hello" in a textarea.我在 textarea 中输入“你好”。

I want the variable to be: x = "Hello"我希望变量为: x = "Hello"

I've tried我试过了

document.getElementById("someId").value; 

which doesn't work for me这对我不起作用

<textarea id="ide" rows="13" cols="60"></textarea>

Seems I didnt explain the question right, I wanted to store the value everytime you type but I found a way:似乎我没有正确解释问题,我想在每次键入时存储该值,但我找到了一种方法:

function val(e) {
     console.log(e.value);
}
<textarea id="ide" rows="13" cols="60" onkeyup="val(this);"></textarea>

and this is the answer in case anyone wondered, thanks for the replies!这是万一有人想知道的答案,感谢您的回复!

function val(e) {
     var someText = e.value;
     console.log(someText )

     if (someText == "text") {  
          document.getElementById("msg").innerHTML="new text"; 
     }
}

 var valueInVar = ''; function textareaToVar(){ valueInVar = document.getElementById("myTextarea").value; alert(valueInVar); }
 <textarea id="myTextarea" onfocusout="textareaToVar()"></textarea>

<!DOCTYPE html>
<html>
<body>

Address:<br>
<textarea id="ide" rows="13" cols="60">Hello</textarea>

<p>Click the button to change the contents of the text area.</p>

<button type="button" onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  var x = document.getElementById("ide").value;
  alert(x);
}
</script>

</body>
</html>

Your ID is different it is "someid" in your javascript and "ide" in your HTML.您的 ID 是不同的,它是您的 javascript 中的“someid”和 HTML 中的“ide”。 You need to change your code to:您需要将代码更改为:

<textarea id="ide" rows="13" cols="60"></textarea>

document.getElementById("ide").value; 

You also need to make sure your DOM is loaded before trying to get the value.在尝试获取值之前,您还需要确保您的 DOM 已加载。

<script type="text/javascript">
window.onload = function() {
    document.getElementById("delete").onclick = function() {myFunction()};

    function myFunction() {
        //your code goes here
        alert('Alert message here');
    }
};
</script>

You need to tell your program when you want the contents of textarea to be put into a variable.当您希望将 textarea 的内容放入变量时,您需要告诉您的程序。

For example, if you want it to happen every time you type something you need to add an 'input' event listener to the textarea:例如,如果您希望每次键入内容时都发生这种情况,则需要向 textarea 添加一个 'input' 事件侦听器:

 let text; let textarea = document.getElementById('ide'); textarea.addEventListener('input', function getText() { text = textarea.value; });

If you want to get the value when a button is clicked, then add click event to that button.如果要在单击按钮时获取值,请向该按钮添加单击事件。

 let text; let textarea = document.getElementById('ide'); let button = document.getElementById("btn"); button.addEventListener('click', function getText() { text = textarea.value; });

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

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