Rails帮助预览文本字段
问题描述:
我正在尝试预览文本区域。但它dosnt工作。Rails帮助预览文本字段
我new.html.erb:
<h1>New post</h1>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
我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 class="actions">
<%= f.submit %>
</div>
<% end %>
<div id="post-preview" style="width:400px;border:1px solid black; height:500px;"></div>
我new.js.erb:
$('#post-preview').html($('#post_body_html').val());
答
对我来说,似乎就像你需要将预览绑定到能够工作的东西一样:
$(document).ready(function() {
// Bind it to some action.
$('.preview').click(function(){
$('#post-preview').html($('#post_body_html').val());
});
});
你甚至可以将它绑定到键盘按下;就像你在StackOverflow
上看到的那样。
Click事件工作实例:http://jsfiddle.net/kuroir/D7cPT/1/ 自动刷新(上的keyDown)工作实例:http://jsfiddle.net/kuroir/D7cPT/3/
我试图创造一些这样的:http://sedition.com/a/2662 – 2011-05-28 18:15:07
您能不能告诉我怎么样在网站上做预览,我链接到 – 2011-05-28 19:13:43
@Rails初学者他只是向你展示了如何在JavaScript中编写预览功能,这正是你想要的。 – 2011-05-29 03:19:39