首页 > 解决方案 > Rails 6:无法让选择助手的 HTML 选项正常工作

问题描述

我有一个问题,我试图在 Rails 6 中查找选择表单助手并相应地实现它。我已经尝试了尽可能多的不同方式来表达它,但我的 html_options 仍然没有触发。

以下是相关代码:

<div class="form-group">
    <%= f.label :assignment_type %>
    <%= f.select(:assignment_type, options_for_select(Assignment.options, params[:type]), {},  {id: 'assign_type'})%>
</div>

<script type="text/javascript">
    $('#assign_type').change(function () {alert("Yayyy!!!")});
</script>

我认为问题不在于 Javascript 部分,因为当我将 id: 'assign_type' 更改为 class: 'hidden' (我有)时,它不会隐藏元素。所以我认为问题在于表单选择助手。让我知道是否需要附加更多代码。

标签: javascriptruby-on-rails

解决方案


首先,您应该能够使用 Chrome 或 Firefox 开发人员工具在 DOM 中看到这一点。

尝试以下方法来创建您的选择:

<%= f.collection_select(:assignment_type, Assignment.options, :second, :first, {}, {id: 'assign_type'}) %>

那应该工作。


推荐阅读