javascript - 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' (我有)时,它不会隐藏元素。所以我认为问题在于表单选择助手。让我知道是否需要附加更多代码。
解决方案
首先,您应该能够使用 Chrome 或 Firefox 开发人员工具在 DOM 中看到这一点。
尝试以下方法来创建您的选择:
<%= f.collection_select(:assignment_type, Assignment.options, :second, :first, {}, {id: 'assign_type'}) %>
那应该工作。
推荐阅读
- aws-lambda - 为什么 Lambda 在 VPC 中配置,与 ec2 一样,无法向实例内的游牧客户端发送 api 请求?
- python-3.x - 如何使用 Python 在现有 AVRO 文件中追加数据
- php - 正则表达式匹配非 .com/.net 域?
- java - 对讲 API 的 Unirest Java POST 请求返回错误 500
- c# - `[FromQuery]` IEnumerable
在 ASP.NET Core 3.1 中解析? - regex - 在 Word 中复制动态范围直到行尾
- javascript - IE11:Web 请求在沙盒 iframe 中不起作用
- javascript - Javascript 大于 (>) 运算符无法正常工作
- database - 我可以使用 Vividus 工具查询哪些数据库
- c# - 如何在启动时使用 dotnet core 3 中的依赖项调用自定义服务?