【问题标题】:Simple Form ID not being detected in jQueryjQuery 中未检测到简单表单 ID
【发布时间】:2019-05-21 13:58:14
【问题描述】:

我在我的 ruby​​ on rails 应用程序中使用了简单的表单 gem。在我的应用中,我还使用 ActiveStorage 来保存图像。

我正在使用Krajee jquery 文件输入

我希望能够使用简单表单 gem 上传图片。这是我的意见:

<%= f.input :image, input_html: { id: :image_input } %>

我应该使用这个命令初始化输入(由文档提供)

<script>

  $(function() {

        // initialize with defaults
        $("#image_input").fileinput();

        // with plugin options
       $("#image_input").fileinput({'showUpload':false, 'previewFileType':'any'});

  });

</script>

jquery 代码没有隐藏输入。我检查了是否安装了 jQuery 并确保它可以正常工作。我想问题出在我创建输入字段的方式上,但我不太确定。

感谢任何帮助。

更新

当我检查页面源时,输入确实具有正确的 id,但是,输入的样式设置为 display: none。我猜这与 Krajee 图书馆有关。

我试过这个帖子:Change id in simple form

没用。

【问题讨论】:

  • 你检查过HTML是否真的包含&lt;input type="image" id="image_input"&gt;
  • 它在那里,让我更新问题,因为输入本身没有显示出来。
  • 我不使用 RoR,所以我不会将此作为答案。但我怀疑应该是&lt;%= f.input :image, id: "image_input" %&gt;

标签: jquery ruby-on-rails simple-form


【解决方案1】:

尝试将 id 保留为字符串而不是符号 像这样的

html: {class: "form-horizontal quote-form", id: "send-quote-form", :multipart => true}) 

【讨论】:

    【解决方案2】:

    试试这个

    $(document).ready(function(){
        // initialize with defaults
        $("#image_input").fileinput();
    
        // with plugin options
        $("#image_input").fileinput({'showUpload':false, 'previewFileType':'any'});
    })
    

    【讨论】:

    • 该死,这也没用 :/ 不过谢谢,我会继续研究的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多