不能使用jQuery和value方法在textarea中设置文本。

时间:2022-11-12 14:58:00

I've got the following markup:

我有以下标记:

<textarea id="hazaa"></textarea>

When I execute the following JavaScript in the console:

当我在控制台执行以下JavaScript时:

$("#hazaa").value

I get the print-out of what's in the box. However, when I try to execute this:

我把盒子里的东西打印出来。然而,当我试图执行这个:

$("#hazaa").value = "shazoo"

the console notifies me back with shazoo but the text in the box doesn't change. Also, subsequent check of what's in the box returns the old, unaltered value.

控制台会用shazoo通知我,但是盒子里的文本不会改变。而且,随后检查盒子里的东西会返回未改变的旧值。

It's been a while since I've done any jQuery-ing so it's probably something fairly obvious but I can't think of any resolution. I've googled for suggestions but the best one I've found actually discusses properties that aren't there! What am I missing?!

已经有一段时间了,因为我已经做过任何一件事了,所以这可能是很明显的事情,但我想不出任何解决办法。我在google上搜索了一些建议,但我发现的最好的建议实际上是讨论了那些不存在的属性!我错过什么呢? !

Executing the following two lines:

执行以下两行:

$("#hazaa").val
$("#hazaa").val()

produces:

生产:

undefined
TypeError: Object # has no method 'val'

未定义类型错误:Object #没有方法'val'

I trust fully that I'm to blame for it but I don't how how to proceed. :)

我完全相信我应该为此负责,但我不知道该怎么做。:)

2 个解决方案

#1


3  

You need to do this -

你需要这样做。

setter

setter

$("#hazaa").val("shazoo");

getter

getter

var val = $("#hazaa").val();

Demo --> http://jsfiddle.net/E3kZy/1/

演示- - > http://jsfiddle.net/E3kZy/1/

#2


0  

You can set value by using .val()

您可以使用.val()来设置值。

Set the value of each element in the set of matched elements.

设置匹配元素集合中的每个元素的值。

   $("#hazaa").val('Your text here');

.val()

.val()

You can get the value using $("#hazaa").val();

您可以使用$(“#hazaa”).val()获得该值;

#1


3  

You need to do this -

你需要这样做。

setter

setter

$("#hazaa").val("shazoo");

getter

getter

var val = $("#hazaa").val();

Demo --> http://jsfiddle.net/E3kZy/1/

演示- - > http://jsfiddle.net/E3kZy/1/

#2


0  

You can set value by using .val()

您可以使用.val()来设置值。

Set the value of each element in the set of matched elements.

设置匹配元素集合中的每个元素的值。

   $("#hazaa").val('Your text here');

.val()

.val()

You can get the value using $("#hazaa").val();

您可以使用$(“#hazaa”).val()获得该值;