简体   繁体   English

用javascript替换textarea内容(原型)

[英]Replacing textarea content with javascript (prototype)

In a Rails application using prototype, I have the following (simplified) code in my view (Edit: replaced with actual HTML output): 在使用原型的Rails应用程序中,我的视图中包含以下(简化)代码(编辑:替换为实际的HTML输出):

<textarea cols="34" id="note" name="note" rows="5"></textarea>
<a href="#" onclick="$('note').update('test note'); return false;">copy note</a>

This works fine on a fresh page visit and does what is intended: Insert "test note" into the text area on clicking the link. 这在重新访问页面时效果很好,并且可以完成预期的操作:单击链接,将“测试注释”插入文本区域。

However, if I then remove or edit the text in the textarea field and click the link again, the text is not replaced again. 但是,如果我随后在textarea字段中删除或编辑文本并再次单击链接,则该文本不会再次替换。 This happens in both Safari and Firefox. 这在Safari和Firefox中都会发生。 No javascript errors are occurring. 没有发生JavaScript错误。

Strangely enough, Firebug does show the text between the tags on these subsequent clicks being replaced again. 奇怪的是,Firebug确实在再次替换这些后续点击时显示了标签之间的文本。

What am I missing? 我想念什么?

EDIT: 编辑:

I found this: http://scott-tabar-safari.blogspot.com/2008/01/prototype-v16-ajaxupdater-and-textarea.html . 我找到了: http : //scott-tabar-safari.blogspot.com/2008/01/prototype-v16-ajaxupdater-and-textarea.html Addding a $('formid').reset() fixes this. 添加$('formid')。reset()可以解决此问题。 Better solutions are still welcome. 仍然欢迎更好的解决方案。

您应该使用textarea DOM对象的value属性:

<a href="#" onclick="$('note').value = 'test note'; return false;">copy note</a>

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

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