开发者

Rails help preview text field

I am trying to preview a text area. But it dosnt work.

My new.html.erb:

<h1>New post</h1>

<%= render 'form' %>

<%= link_to 'Back', posts_path %>

My form.html.erb:

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :titel %><br />
    <%= f.text_field :titel %>
  </div>
  <div class="field">
    <%= f.label :body_html %><br />
    <%= f.text_area :body_html %>
  </div>
  <div class="field">
    <%= f.label :up_votes %><br />
    <%= f.text_field :up_votes %>
  </div>
  <div class="field">
    <%= f.label :down_votes %><br />
    <%= f.text_field :down_votes %>
  </div>
  <div cl开发者_JS百科ass="actions">
    <%= f.submit %>
  </div>
<% end %>
<div id="post-preview" style="width:400px;border:1px solid black; height:500px;"></div>

My new.js.erb:

$('#post-preview').html($('#post_body_html').val());


To me it seems like you need to bind the preview to something to be able to work:

$(document).ready(function() {
    // Bind it to some action.
    $('.preview').click(function(){
        $('#post-preview').html($('#post_body_html').val());
    });
});

You can even bind it to keyboard pressing; like the one you see here on StackOverflow.

Click event Working Example: http://jsfiddle.net/kuroir/D7cPT/1/ Automatic refresh (on keyDown) working Example: http://jsfiddle.net/kuroir/D7cPT/3/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜