简体   繁体   中英

Get textarea value from textarea object

Why doesn't this work?. Here the input type is text :

var name = $("input[name='Event[name]']").serializeArray();
name = name[0].value;

var description = $("input[name='Event[description]']").serializeArray();
description = description[0].value; 

When I want to get the from a textarea instead, it doesn't work.

This should work:

var name = $("input[name='Event[name]']").val();

var description = $("input[name='Event[description]']").val();

Let jQuery handle value .

The .val() method is primarily used to get the values of form elements such as input, select and textarea .

将您的代码从.val()替换为.text()

value isn't a property of a textarea. Textarea's are nodes that have content. Use the JQuery attribute for textContent.

A textarea as no value attribute. Use $("input[name='Event[description]']").val() since jQuery folds all values for all kind of input elements (input, select, checkbox and textarea) into this function call.

That means you should always use .val() to get the value for anything - your code will be more simple and you can even change the type of an input element without breaking anything.

Is there any particular reason as to why you use get value as array?

If not see below :

var name    = $('#name').val();
var description = $('textarea#description').val();

considering name and description is what you have given as id for text field and text area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM