简体   繁体   English

如何在JavaScript中检索文本区域的值

[英]How to retrieve the value of a text area in javascript

I have a form with a text area input. 我有一个带有文本区域输入的表格。 I'm using JQuery to submit the form via an AJAX request in order to update a database. 我正在使用JQuery通过AJAX请求提交表单以更新数据库。 My problem is that I'm having difficulty retrieving the data from the text area input. 我的问题是我很难从文本区域输入中检索数据。 If the input has an id of "txtBody" I have tried: 如果输入的ID为“ txtBody”,我尝试过:

var body = $("#txtBody").val(); // This adds 'undefined' to the database  
var body = $("#txtBody").text(); // This adds nothing to the database  
var body = $("#txtBody").html(); // This adds 'NULL' to the database  

I can't think of how else to access the data. 我想不出其他方式如何访问数据。 Any ideas? 有任何想法吗?

You say adds to the database. 您说添加到数据库。 Have you debugged the actual code to make sure you're not just sending the data with one variable name and trying to add it with another? 您是否已调试实际的代码,以确保您不仅在发送带有一个变量名的数据,并尝试向另一个变量名添加数据? Because if you have a field like this: 因为如果您有这样一个字段:

<input type='text' id='txtBody' value='test'>

Or like this: 或像这样:

<textarea id='txtBody'>test</textarea>

Doing $('#txtBody').val(); $('#txtBody').val(); will return the value "test". 返回值“ test”。 There's no ifs or buts about it. 毫无疑问。

Maybe you should post some more of your code so we can spot what is wrong, as I am guessing that's not the actual problem you are having. 也许您应该发布更多代码,以便我们找出问题所在,因为我猜这并不是您遇到的实际问题。

The jQuery documentation suggests that val() was not available is older versions of jQuery. jQuery文档建议val()在旧版本的jQuery中不可用。 Is your version up to date? 您的版本是最新的吗?

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

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