简体   繁体   English

使用onHTML事件中的innerHTML设置文本区域的内容仅适用一次

[英]Setting content of a textarea with innerHTML from an onclick event only works once

I'm trying to update the content of a <textarea> field when a <button> is clicked with the onclick="" event. 我尝试通过onclick=""事件单击<button>时更新<textarea>字段的内容。

My problem is that the button only works once. 我的问题是该按钮只能使用一次。 If I change the content of the text area the button doesn't work anymore. 如果我更改文本区域的内容,该按钮将不再起作用。

I've found several questions with the same problem but they all use variable inputs or not random data. 我发现了几个问题相同的问题,但是它们都使用变量输入或不使用随机数据。

You can see an example here: 您可以在此处查看示例:

  1. Click on the Add test data button 点击添加测试数据按钮
  2. Modify the content of the textarea 修改文本区域的内容
  3. Repeat step 1.: The button doesn't work but the console register the done message 重复步骤1:按钮不起作用,但控制台会记录已完成的消息

 <script> function addExample() { document.getElementById('fasta').innerHTML = 'acgt'; console.log('done'); } </script> <p> <button type="button" onclick="addExample();">Add test data</button> </p> <textarea name="fasta" id="fasta"></textarea> 

I'm using Firefox 44.0 on Ubuntu 15.04. 我在Ubuntu 15.04上使用Firefox 44.0。

PS: I've found several questions with the same problem on SO but they all use variable inputs or not random data. PS:我在SO上发现了几个相同问题的问题,但它们都使用变量输入或不使用随机数据。

不要使用innerHTML,而是使用value属性。

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

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