Javascript 如何在jquery中获取textarea的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10507294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the value of a textarea in jquery?
提问by Patrioticcow
i have this form and im trying to get the value from the text area. for some reason it doesn't want to.
我有这个表格,我试图从文本区域获取值。出于某种原因,它不想。
<form action="/profile/index/sendmessage" method="post" enctype="application/x-www-form-urlencoded">
<div class="upload_form">
<dt id="message-label"><label class="optional" for="message">Enter Message</label></dt>
<dd id="message-element">
<textarea cols="60" rows="5" id="message" name="message"></textarea></dd>
<dt id="id-label"> </dt>
<dd id="id-element">
<input type="hidden" id="id" value="145198" name="id"></dd>
<dt id="send_message-label"> </dt>
<dd id="send_message-element">
<input type="submit" class="sendamessage" value="Send" id="send_message" name="send_message"></dd>
</div>
</form>
$("input.sendamessage").click(function(event) {
event.preventDefault();
var message = $('textarea#message').html();
var id = $('input#id').val();
console.log(message + '-' + id);
});
or jsfiddle
any ideas?
有任何想法吗?
回答by VisioN
Value of textarea is also taken with val
method:
textarea 的值也通过val
方法获取:
var message = $('textarea#message').val();
回答by Selvakumar Arumugam
You need to use .val()
for textarea as it is an element and not a wrapper. Try
您需要使用.val()
for textarea 因为它是一个元素而不是包装器。尝试
$('textarea#message').val()
回答by undefined
you should use val()
instead of html()
你应该使用val()
而不是html()
var message = $('#message').val();
回答by Saurabh Chandra Patel
in javascript :
在 JavaScript 中:
document.getElementById("message").value
回答by SidTechs1
You don't need to use textarea#message
你不需要使用 textarea#message
var message = $('textarea#message').val();
You can directly use
你可以直接使用
var message = $('#message').val();
回答by Wuji
You should check the textarea is null before you use val() otherwise, you will get undefined error.
在使用 val() 之前,您应该检查 textarea 是否为空,否则,您将收到未定义的错误。
if ($('textarea#message') != undefined) {
var message = $('textarea#message').val();
}
Then, you could do whatever with message.
然后,您可以对消息执行任何操作。
回答by Zbyszek Swirski
$('textarea#message')
cannot be undefined (if by $
you mean jQuery of course).
$('textarea#message')
不能是未定义的($
当然,如果您指的是 jQuery)。
$('textarea#message')
may be of length 0 and then $('textarea#message').val()
would be empty that's all
$('textarea#message')
可能长度为 0 然后$('textarea#message').val()
是空的
回答by jonathan klevin
You can directly use
你可以直接使用
var message = $.trim($("#message").val());
Read more @ Get the Value of TextArea using the jQuery Val () Method
回答by rahim.nagori
You can also get the value by element's name attribute.
您还可以通过元素的name 属性获取值。
var message = $("#formId textarea[name=message]").val();
回答by Jawwad Ali Khan
You don't need to use .html()
. You should go with .val()
.
您不需要使用.html()
. 你应该和.val()
.
From the doc of .val()
:
从文档.val()
:
The
.val()
method is primarily used to get the values of form elements such asinput
,select
andtextarea
. When called on an empty collection, it returnsundefined
.
该
.val()
方法主要用于获取input
,select
和等表单元素的值textarea
。当在一个空集合上调用时,它返回undefined
.
var message = $('#message').val();