1

I am embedding tinymce [http://www.tinymce.com/] in twitter bootstrap form , so that user can input formatted text which I can persist to a postgres database.

The problem I am facing is that I get plain text on submit of the form. Can someone help me get the formatted text as a string that I can persist and echo to the html page .

Here is my code [Note:It uses HAML]

%html{:lang => "en"}
  %head
    %meta{:charset => "utf-8"}
        %link{:href => "//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css", :rel => "stylesheet"}
    %script{:src => "//tinymce.cachefly.net/4.0/tinymce.min.js"}
    :javascript
      tinymce.init({selector:'textarea_format'});
%body
  .container
    %legend Post a New Job
    .well
      %form#signup.form-horizontal{:action => "submit", :method => "post"}
        %legend
        .control-group
          %label.control-label Title
          .controls
            .input-prepend
              %span.add-on
                %i.icon-user
              %input#title.input-xxlarge{:name => "title", :placeholder => "Title", :type => "text"}/
        .control-group
          %label.control-label Description Formatted
          .controls
            .input-prepend
              -#%span.add-on
                -#%i.icon-user
              %textarea_format#message.input-xlarge.span6{:name => "message_formatted", :rows => "10"}
        .control-group
          %label.control-label
          .controls
            %button.btn.btn-success{:type => "submit"} Submit 

Seems like I can use tinyMCE.get('content id').getContent() but I am not sure how ?

I am very new to java-script . Can someone please post the required code change and some explanation.

Thanks

4

1 回答 1

1

基本上,最简单的方法是在表单中添加一个隐藏字段并在提交按钮上添加一个单击事件处理程序。

因此,在您的格式上创建一个名为“message_formatted”的隐藏输入(因为我想您模型中的相应字段称为 message_formatted),并将您的 textarea 的名称更改为其他名称,因为这将不再重要。

使用 jQuery:

$('#signup input[type=submit]').click(function(e){
  $('input[name=message_formatted]').val(tinyMCE.get('content id').getContent());
});

至于在哪里添加 javascript,这取决于您。最好将它放在 javascripts 目录中(不清楚您是否使用 Rails 和资产管道)。如果您只想在这个haml页面中添加javascript内联,请将上面的content_for :javascript do内容放在页面底部的

content_for :javascript do
  // enter the javascript from above here
于 2013-07-10T13:20:56.147 回答