我有一个视图,其中包含select
标签,它不是表单的一部分,只是一个独立的下拉菜单。单击其中一个选项时,我想执行 ajax 调用
这是选择标签
<%= select_tag 'application_stage', options_for_select(application.job.hiring_procedure.hiring_procedure_stages.map{ |p| [p.name, p.id] }), prompt: "Hiring stage", class: 'input-block-level chzn-select hiring_stage_dropdown', id: 'hiring_stage_dropdown' %>
这是一个基本的 jquery,我试图通过它来检测变化
jQuery(function($) {
console.log('clicked');
$("#hiring_stage_dropdown").click(function() {
var state = $('select#hiring_stage_dropdown :selected').val();
console.log(state);
});
})
但是,当我选择其中一个选项时,什么也没有发生,我知道 ajax 代码不存在,但我现在想要的只是在选择其中一个选项时在控制台中看到一些内容。
我在这里缺少什么?
这就是选择的样子
<select class="input-block-level chzn-select hiring_stage_dropdown" id="hiring_stage_dropdown" name="application_stage">
<option value="">Hiring stage</option>
<option value="3">Manager Step 1</option>
<option value="4">Manager Step 2</option>
</select>
从选择下拉列表中选择项目时执行 AJAX 调用是通过 jquery-ujs 内置到 Rails 中的。
首先,确保你有gem 'jquery-rails'
在你的 Gemfile 中并且//= require jquery_ujs
在你的 application.js 清单文件中(默认情况下它们都在那里)。
然后加data-remote
, data-url
, and data-method
您选择的属性。 jquery-url 将序列化 select 的值并发出请求。你的select_tag
代码可能如下所示:
<%= select_tag(
'application_stage',
options_for_select(
application.job.hiring_procedure.hiring_procedure_stages.map{ |p| [p.name, p.id] }
),
prompt: "Hiring stage",
class: 'input-block-level chzn-select hiring_stage_dropdown',
id: 'hiring_stage_dropdown',
data: {
remote: true,
url: '/your_path',
method: 'get'
}
) %>
当选择一个值时,jquery-ujs 会生成一个GET
请求/your_path?application_stage=:selected_value
。然后您可以在 js.erb 响应中处理此问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)