我遇到了一个问题,尝试在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'}) %>
应该起作用。