如何检索一个文本区在JavaScript中的价值文本、价值、JavaScript

2023-09-10 18:25:15 作者:哭到世界都心碎

我有一个文本区输入的形式。我使用jQuery通过一个Ajax请求,以更新数据库提交表单。我的问题是,我有困难,从文本区输入检索数据。如果输入有txtBody的ID我都试过:

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. 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:

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?

推荐答案

您说增加了数据库。你调试的实际code,以确保你不只是发送数据有一个变量名,并尝试与另一增加吗?因为如果你有一个领域是这样的:

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>

$('#txtBody)VAL(); 的将会的返回值测试。有没有如果或但是这件事。

Doing $('#txtBody').val(); will return the value "test". There's no ifs or buts about it.

也许你应该张贴一些你更多的code,所以我们可以发现什么是错的,因为我猜这不是你拥有的是实际的问题。

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.