简体   繁体   English

如何使用JavaScript在textarea标记中提供输入?

[英]How to get input provided in a textarea tag using JavaScript?

Have a textarea tag in form, to take input text and return the value of said text. 具有textarea标记形式,以获取输入文本并返回所述文本的值。

 <textarea id="code" name="code"></textarea>  

Using javascript to get the value, but it returns nothing. 使用javascript获取值,但不返回任何内容。 Used typeof to check if it's undefined , but typeof text gives 'string' . 使用typeof来检查它是否undefined ,但是typeof text给出'string' Is this correct way to use .value ? 这是使用.value正确方法吗? How to improve this? 如何改善呢?

JavaScript: JavaScript的:

var text = document.getElementById('code').value;

document.getElementById('submit-button').onclick = function() {
    alert(text);
}; 

You're getting the value immediately when the page loads, before the user has had a chance to type anything. 当用户有机会键入任何内容时,您将在页面加载后立即获得该值。 You want to get the value when the user clicks the button: 您想在用户单击按钮时获取值:

 document.getElementById('submit-button').onclick = function() { var text = document.getElementById('code').value; alert(text); }; 
 <textarea id="code" name="code"></textarea> <button type="button" id="submit-button">click me</button> 

You need to bind the event and get the value just when the user clicks the button. 您需要绑定事件并仅在用户单击按钮时获取值。

Embrace the function addEventListener 拥抱功能addEventListener

 document.getElementById('submit-button').addEventListener('click', function() { var text = document.getElementById('code').value; console.log(text); }); 
 <textarea id="code" name="code"></textarea> <button type="button" id="submit-button">click me</button> 

Stick the var text line inside the click event; var text行粘贴在click事件中; it's getting defined when the script runs originally, so it is just an empty string. 它是在脚本最初运行时定义的,因此它只是一个空字符串。

document.getElementById('submit-button').onclick = function() {
    var text = document.getElementById('code').value;
    alert(text);
};

暂无
暂无

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

相关问题 我如何验证 <input type = “date”> 和 <textarea></textarea> 使用javascript标记 - How can i validate <input type = “date”> and <textarea></textarea> tag using javascript 如何使用javascript在textarea中输入Html编辑器 - How to get input of Html editor in textarea using javascript 如何使用javascript填充textarea标签 - How to fill textarea tag using javascript 如何从textarea获取输入到javascript函数? - How to get input from textarea to javascript function? 如何使用Javascript为输入或textarea中的数字加下划线 - How to underline numbers in an input or textarea using Javascript 如何在 textarea(HTML 标记)中写入 JavaScript object 以使用 documentQuerySelector 获取 object - how to write JavaScript object in the textarea(HTML Tag) to get that object by using documentQuerySelector 如何使用 jQuery 在甜蜜警报中添加 textarea 标签作为输入元素 - How to add textarea tag as input element in sweet alert using jQuery 如何使用html输入标签设置文本区域中文本的颜色 - How to set the colour of a text in a textarea using html input tag 如何使用 javascript 按字母顺序对 textarea 标签进行排序并在另一个 textarea 标签中输出? - how could a textarea tag be sorted alphabeticaly and outputted in another textarea tag using javascript? 如何获取 WYSIWYG 的值并将其仅插入 Javascript 的 textarea 标签中? - How to get value of WYSIWYG and insert it in textarea Tag by Javascript only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM