How to retrieve the value of a text area in javasc

2019-06-03 23:56发布

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?

2条回答
Summer. ? 凉城
2楼-- · 2019-06-04 00:27

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(); will return the value "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.

查看更多
叛逆
3楼-- · 2019-06-04 00:41

The jQuery documentation suggests that val() was not available is older versions of jQuery. Is your version up to date?

查看更多
登录 后发表回答