问题
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
回答1:
Basically, the simplest way to do this is through adding a hidden field to your form and a click event handler on the submit button.
so, create a hidden input on your format, with the name "message_formatted" (since I imagine your corresponding field in your model is called message_formatted), and change the name of your textarea to something else, as that will no longer be important.
using jQuery:
$('#signup input[type=submit]').click(function(e){
$('input[name=message_formatted]').val(tinyMCE.get('content id').getContent());
});
In terms of where to add the javascript, that's up to you. It's best to place it in a javascripts directory (it's unclear whether you're using Rails and the asset pipeline or not). If you want to just add the javascript inline within this haml page, place the above within the content_for :javascript do
at the bottom of your page
content_for :javascript do
// enter the javascript from above here
来源:https://stackoverflow.com/questions/17247900/save-formatted-text-to-database-and-retrieve-it-back-as-is-like-a-formatted-st