简体   繁体   English

JavaScript 中的文本区域不能使用.value

[英]Can't use .value for Text Area in JavaScript

I'm trying to get the value of a text area after the submit button is pressed.我试图在按下提交按钮后获取文本区域的值。 However, when I try to use .value in my JS file to get the textarea, it doesn't work.但是,当我尝试在我的 JS 文件中使用.value来获取 textarea 时,它不起作用。 Here is my code for my HTML text area and button:这是我的 HTML 文本区域和按钮的代码:

<form>
        <textarea type="text" id = "textArea" rows="5" cols="100"></textarea>
        <br>
        <input type="button" id="submitButton" value="Submit">
</form>

And then this is my Javascript code for trying to get the value of the text area.然后这是我的 Javascript 代码,用于尝试获取文本区域的值。 I've set up a listener on the submit button.我在提交按钮上设置了一个监听器。

var submit = document.getElementById("submitButton")
function buttonPressed(){
     var text = document.getElementById("textArea").value (this .value part doesn't show up)
     console.log(text);   
     submit.addEventListener("click", buttonPressed())
}

The .value part won't work and doesn't show up when I type it. .value部分不起作用,并且在我键入时不会显示。 The only thing that does show up is .nodeValue How would I get the value of this text area?唯一显示的是.nodeValue我如何获得这个文本区域的值?

I'm a newb at Javascript so any help would be appreciated.我是 Javascript 的新手,因此我们将不胜感激。 Thank you.谢谢你。

your problem is that your button doesn't have attached the event listener because the "addEventListener is inside the function (that is never invoked), I extracted this and the function is invoked now.您的问题是您的按钮没有附加事件侦听器,因为“addEventListener 在 function 内(从未调用过),我提取了它,现在调用了 function。

 const submit = document.getElementById("submitButton"); submit.addEventListener("click", buttonPressed) function buttonPressed(){ const text = document.getElementById("textArea").value console.log(text); }
 <form> <textarea type="text" id = "textArea" rows="5" cols="100"></textarea> <br> <input type="button" id="submitButton" value="Submit"> </form>

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

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