我在尝试在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'(我拥有)时,它不会隐藏该元素。因此,我认为问题出在表单选择助手中。让我知道是否需要附加更多代码。

最佳答案

首先,您应该可以使用Chrome或Firefox开发人员工具在DOM中看到此内容。

尝试以下操作来创建您的选择:

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


那应该工作。

关于javascript - Rails 6:无法获得选择助手的HTML选项以使其正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59805751/

10-12 16:56
查看更多