【问题标题】:myFunction is not defined with ActiveAdmin form & RailsmyFunction 未使用 ActiveAdmin 表单和 Rails 定义
【发布时间】:2015-10-07 04:49:50
【问题描述】:

我不明白我为什么会遇到这个问题。好像我的函数 hiddenField() 没有被读取。

我的表格:

f.input :contact, :as => :radio, :collection => ["slide", "formulaire de contact", "map", "video"], input_html: {:class => "select", :onblur => "hiddenField()"}
f.input :title_map, label: I18n.t('title_map'), input_html: {:class => "hidden_title"}, placeholder: "Entrer un titre pour la map"

我的脚本:

var ready;
ready = function() {

    function hiddenField() {
        $(".select").on( 'click', function() {
            document.getElementsByClassName("hidden_title").removeAttribute("input");
        };
    };

    hiddenField();
};

$(document).ready(ready);
$(document).on('page:load', ready);

谢谢。

编辑:

对不起,我忘记了。我的表格在 admin/page.rb 中。我使用 ActiveAdmin。

【问题讨论】:

  • 因为你已经在另一个函数内部定义了函数,所以不能从外部访问它
  • 将函数hiddenField放在ready()之外,或者在ready()末尾添加window.hiddenField = hiddenField
  • 我在外面也有同样的问题...

标签: javascript ruby-on-rails forms activeadmin


【解决方案1】:
$(function() {
  $('.select').change(function() {
    $('.hidden_title').removeAttr('input')
  })
})

【讨论】:

  • 您能否就如何解决 OP 的问题添加一些上下文?
  • 你是对的@ilan。 onblur 事件没用。我的代码有点乱,但现在可以了。我发现它需要在 config/initializers/active_admin.rb 中添加 config.register_javascript 'myFunction.js' 才能使此代码有效。谢谢。
猜你喜欢
  • 2019-10-27
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
相关资源
最近更新 更多