【问题标题】:Image Preview in Rails View with JS使用 JS 在 Rails 视图中预览图像
【发布时间】:2012-04-04 20:08:03
【问题描述】:

我有一个 text_field,它是图像的 url,我想将它加载到 img 标记中。我还想在 image_url 文本框失去焦点时更新图像,以便他们可以更改 url 并查看预览。

这是视图的相关 erb:

 <div class="control-group">
    <%= f.label :image, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :image_url, :class => 'text_field', id: "image_url" %>
    </div>
  </div>

  Preview: <img id="preview" src="">

还有嵌入的js...

<script type="text/javascript">

document.observe('dom:loaded', function() {
  $('preview').src = $('image_url').text
  $('image_url').bind("focusout",function(){
    $('preview').src = this.text
    )}

});

加载时我什么也得不到,src 也没有改变

【问题讨论】:

    标签: javascript ruby-on-rails image view erb


    【解决方案1】:

    您需要指定要查找的对象的选择器。我也会使用准备好的文档。

    $(document).ready(function() {
      $('#preview').src = $('#image_url').text();
      $('#image_url').bind("focusout",function(){
        $('#preview').src = this.text();
      });
    });
    

    【讨论】:

    • 通过“指定对象的选择器”,Mike 表示您的 id 选择器前面缺少#
    猜你喜欢
    • 2018-08-18
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多