如何在javascript中检索文本区域的值 [英] How to retrieve the value of a text area in javascript

查看:114
本文介绍了如何在javascript中检索文本区域的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本区域输入的表单。我正在使用JQuery通过AJAX请求提交表单,以更新数据库。我的问题是我无法从文本区域输入中检索数据。如果输入的id为txtBody,我试过:

  var body = $(#txtBody)。val (); //将数据库'undefined'添加到
var body = $(#txtBody)。 //对数据库没有添加
var body = $(#txtBody)。html(); //将数据库'NULL'添加到数据库

我无法想像如何访问数据。任何想法?

解决方案

你说添加到数据库。您是否调试了实际的代码,以确保您不仅仅是用一个变量名发送数据,并尝试用另一个变量名来添加它们?因为如果你有这样的字段:

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

或像这样:

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

执行 $('#txtBody')。val() code> 返回值test。没有ifs或buts关于它。



也许你应该发布更多的代码,所以我们可以发现什么是错的,因为我猜这不是你的实际问题正在拥有。


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?

解决方案

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.

这篇关于如何在javascript中检索文本区域的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆