简体   繁体   English

从textarea对象获取textarea值

[英]Get textarea value from textarea object

Why doesn't this work?. 为什么不起作用? Here the input type is text : 这里的input类型是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. 当我想从textarea获取时,它不起作用。

This should work: 这应该工作:

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

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

Let jQuery handle value . 让jQuery处理value

The .val() method is primarily used to get the values of form elements such as input, select and textarea . .val()方法主要用于获取表单元素的值,例如input,select和textarea

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

value isn't a property of a textarea. 值不是textarea的属性。 Textarea's are nodes that have content. Textarea是具有内容的节点。 Use the JQuery attribute for textContent. 将JQuery属性用于textContent。

A textarea as no value attribute. textarea作为无value属性。 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. 使用$("input[name='Event[description]']").val()因为jQuery将所有类型的输入元素 (输入,选择,复选框和文本区域)的所有值折叠到此函数调用中。

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. 这意味着您应该始终使用.val()来获取任何值-您的代码将更加简单,甚至可以更改输入元素的类型而不会破坏任何内容。

Is there any particular reason as to why you use get value as array? 关于为什么将get value用作数组,是否有任何特殊原因?

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. 考虑名称描述就是您为文本字段和文本区域指定的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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