如何在jquery中获取文本区域的值?

时间:2022-11-12 14:53:48

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">&nbsp;</dt>
        <dd id="id-element">
        <input type="hidden" id="id" value="145198" name="id"></dd>
        <dt id="send_message-label">&nbsp;</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

或jsfiddle

any ideas?

什么好主意吗?

11 个解决方案

#1


101  

Value of textarea is also taken with val method:

用val方法取文本区域的值:

var message = $('textarea#message').val();

#2


19  

You need to use .val() for textarea as it is an element and not a wrapper. Try

您需要对textarea使用.val(),因为它是一个元素而不是包装器。试一试

$('textarea#message').val()

Updated fiddle

更新的小提琴

#3


15  

you should use val() instead of html()

应该使用val()而不是html()

var message = $('#message').val();

#4


4  

in javascript :

在javascript中:

document.getElementById("message").value

#5


4  

You don't need to use textarea#message

您不需要使用textarea#message

var message = $('textarea#message').val();

You can directly use

你可以直接使用

var message = $('#message').val();

#6


3  

You should check the textarea is null before you use val() otherwise, you will get undefined error.

在使用val()之前,应该检查textarea是否为null,否则会得到未定义的错误。

if ($('textarea#message') != undefined) {
   var message = $('textarea#message').val();
}

Then, you could do whatever with message.

然后,你可以用信息做任何事。

#7


1  

$('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()将为空,仅此而已

#8


1  

You can directly use

你可以直接使用

var message = $.trim($("#message").val());

Read more @ Get the Value of TextArea using the jQuery Val () Method

使用jQuery Val()方法读取更多@获取文本区域的值

#9


0  

all Values is always taken with .val();

所有值总是与.val()一起取值;

see the code bello

贝洛查看代码

var message = $('#message').val();

#10


0  

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 as input, select and textarea. When called on an empty collection, it returns undefined.

.val()方法主要用于获取表单元素的值,如input、select和textarea。当调用空集合时,它返回未定义的。

var message = $('#message').val();

#11


0  

You can also get value by name instead of id like this:

你也可以通过名字而不是像这样的id来获取值:

var message = $('textarea:input[name=message]').val();

#1


101  

Value of textarea is also taken with val method:

用val方法取文本区域的值:

var message = $('textarea#message').val();

#2


19  

You need to use .val() for textarea as it is an element and not a wrapper. Try

您需要对textarea使用.val(),因为它是一个元素而不是包装器。试一试

$('textarea#message').val()

Updated fiddle

更新的小提琴

#3


15  

you should use val() instead of html()

应该使用val()而不是html()

var message = $('#message').val();

#4


4  

in javascript :

在javascript中:

document.getElementById("message").value

#5


4  

You don't need to use textarea#message

您不需要使用textarea#message

var message = $('textarea#message').val();

You can directly use

你可以直接使用

var message = $('#message').val();

#6


3  

You should check the textarea is null before you use val() otherwise, you will get undefined error.

在使用val()之前,应该检查textarea是否为null,否则会得到未定义的错误。

if ($('textarea#message') != undefined) {
   var message = $('textarea#message').val();
}

Then, you could do whatever with message.

然后,你可以用信息做任何事。

#7


1  

$('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()将为空,仅此而已

#8


1  

You can directly use

你可以直接使用

var message = $.trim($("#message").val());

Read more @ Get the Value of TextArea using the jQuery Val () Method

使用jQuery Val()方法读取更多@获取文本区域的值

#9


0  

all Values is always taken with .val();

所有值总是与.val()一起取值;

see the code bello

贝洛查看代码

var message = $('#message').val();

#10


0  

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 as input, select and textarea. When called on an empty collection, it returns undefined.

.val()方法主要用于获取表单元素的值,如input、select和textarea。当调用空集合时,它返回未定义的。

var message = $('#message').val();

#11


0  

You can also get value by name instead of id like this:

你也可以通过名字而不是像这样的id来获取值:

var message = $('textarea:input[name=message]').val();