【问题标题】:How to set value in input type text with value in textarea using javascript? [duplicate]如何使用javascript在文本区域中设置输入类型文本中的值? [复制]
【发布时间】:2018-01-13 16:18:54
【问题描述】:

对于这段代码,它工作得很好:

<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script>
<textarea style="height: 300px;"></textarea>
<p id="preview"></p>        
<script type="text/javascript">
setInterval(function() {
    $('#preview').html($('textarea').val());
}, 10);
</script>

但是当我像这样从&lt;p&gt; 更改为&lt;input 时:

<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script>
<textarea style="height: 300px;"></textarea>
<input type="text" id="preview">
<script type="text/javascript">
setInterval(function() {
    $('#preview').html($('textarea').val());
}, 10);
</script>

为什么它不起作用?我该如何解决?

【问题讨论】:

  • 在这里分享你的代码
  • 因为您需要更改输入的值而不是其中的 html

标签: javascript


【解决方案1】:

像这样使用 .val 代替 .html:

<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.js"></script>
<textarea style="height: 300px;"></textarea>
  <input type="text" id="preview">
<script type="text/javascript">
   setInterval(function() {
   $('#preview').val($('textarea').val());
    }, 10);
</script>       

【讨论】:

    猜你喜欢
    • 2022-12-05
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多