[英]Setting text of text area in javascript
为什么这不能设置文本区域的文本?
document.getElementById("code").value = "hey farmer";
我甚至尝试过: document.getElementById('code').value = "hey farmer";
有没有办法使用getElementByName来设置值?
任何帮助表示赞赏
[HTML]
<form>
<textarea id="code" name="code">
Lollllllll
</textarea>
</form>
它工作得很好。 在
<textarea id=code>Foo</textarea>
<button type=button onclick="document.getElementById('code').value = 'Bar'">
单击按钮时,文本将更改为“Bar”。
获取时,value属性必须返回元素的API值; 在设置时,必须将元素的原始值设置为新值 ,将元素的脏值标志设置为true,然后将文本输入光标位置移动到文本字段的末尾,取消选择任何选定的文本并重置选择方向没有。
所以这是行为。
问题可能是你的代码运行时有多个(或零)元素id="code"
,所以它得到了错误的元素。
另一个问题可能是你的<textarea>
是不可变的。
http://www.w3.org/TR/html5/forms.html说
如果textarea元素既未被禁用也未指定readonly属性,则该元素是可变的。
原生API
document.getElementById("id").value="Radha Mohan"
jQuery API
$("#couponRestrictionBean\\.includeProductIds").val("Radha")
<form>
<textarea id="code" name="code">
Lollllllll
</textarea>
</form>
替换为:
<form name="yourform">
<textarea id="code" name="code">
Lollllllll
</textarea>
</form>
document.forms['yourform']['code'].value = 'yourvalue';
如果你在你的html之后粘贴你的脚本,它将确定你的工作。 以下是工作代码:
http://jsbin.com/EDOJEZ/2/edit?html,output
我再说一次,你必须在你的html标记之后放置你的脚本块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.