简体   繁体   English

更改文本区域值的命令按钮

[英]Command button that changes the value of a textarea

In order to answer this other question ( Use Froala Editor with JSF ), I need to pass the content of a div similar to this: 为了回答另一个问题( 将Froala Editor与JSF结合使用 ),我需要传递类似于以下内容的div内容:

<div class="fr-view">
  Aaaaaaaaaaaaaaaaaa
</div>

To the value of the following inputTextarea (#{newArticleController.body}) when the commandButton is clicked. 单击commandButton时,为以下inputTextarea的值(#{newArticleController.body})。

<h:form id="formId">
  <h:inputTextarea id="articleBody" value="#{newArticleController.body}" styleClass="articleTextarea"/>
  <h:commandButton value="Save" action="#{newArticleController.newArticle}"/>
</h:form>

I have tried the following but it doesn't work: 我尝试了以下方法,但不起作用:

   <script>
     function updateBody() {
       var body = document.getElementById('formId:articleBody');
       var div = $('.fr-view');
       body.value = div.value;
     }
   </script>

   ...

   <h:form id="formId">
     <h:inputTextarea id="articleBody" value="#{newArticleController.body}" styleClass="articleTextarea"/>
     <h:commandButton value="Save" action="#{newArticleController.newArticle}" onclick="updateBody();"/>
   </h:form>

I always receive null in the body variable. 我总是在body变量中收到null。 Any idea on how to solve it? 关于如何解决的任何想法?

Sine your $('.fr-view') is a div $('.fr-view') will not work. 正弦您的$('。fr-view')是div $('.fr-view')将不起作用。 To get the text or innerHtml of a div you need to use $('fr-view').html() or $('fr-view').text() 要获取div的文本或innerHtml ,您需要使用$('fr-view').html()$('fr-view').text()

Finally I solved it without the onclick javascript code. 最后,我没有使用onclick javascript代码就解决了它。 I was putting pt:name="content" in the inputTextarea. 我将pt:name =“ content”放在inputTextarea中。 When I removed it, it worked. 当我删除它时,它起作用了。

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

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