Javascript Rails帮助预览文本字段
我正在尝试预览文本区域。但它不起作用 My new.html.erb:Javascript Rails帮助预览文本字段,javascript,jquery,ruby-on-rails,ruby,ruby-on-rails-3,Javascript,Jquery,Ruby On Rails,Ruby,Ruby On Rails 3,我正在尝试预览文本区域。但它不起作用 My new.html.erb: <h1>New post</h1> <%= render 'form' %> <%= link_to 'Back', posts_path %> <%= form_for(@post) do |f| %> <% if @post.errors.any? %> <div id="error_explanation">
<h1>New post</h1>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
<%= 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>
在我看来,您似乎需要将预览绑定到某个东西才能正常工作:
$(document).ready(function() {
// Bind it to some action.
$('.preview').click(function(){
$('#post-preview').html($('#post_body_html').val());
});
});
您甚至可以将其绑定到键盘按键;就像你在这里看到的StackOverflow
一样
单击事件工作示例:
自动刷新(按下键时)工作示例:你能告诉我如何在我链接的网站上进行预览吗to@Rails初学者他刚刚向你展示了如何用javascript编写预览函数,这正是你想要的。我不需要预览按钮作为示例如果你阅读,你会注意到另一个例子,它正是你想要的。。“自动刷新(按下键时)工作示例:”
$(document).ready(function() {
// Bind it to some action.
$('.preview').click(function(){
$('#post-preview').html($('#post_body_html').val());
});
});