Rails - 动态更改表单中的选项
Rails - dynamically change options in forms
我目前有一个创建 clinicians
的表单。在这个表格上,我想有一个 clinician_type
的下拉列表,用户可以在 physician
、nurse
和 other
之间 select。根据用户的 selection,表单将适应允许他们 select a Speciality
for a physician
或 Occupation
for a nurse
.
我试图实现 23459948 的答案并使其适应具有三个选项,但我不知道 javascript 并且我无法让它工作。
Clinician
belongs_to
:occupation
和 :speciality
和 Occupation
和 Speciality
每个 has_many
:clinicians
。
我在表单中实现此功能的代码如下所示:
<%= form_for @clinician do |form| %>
Type of new clinician: <select id="clinician_type">
<option value="physician">Physician</option>
<option value="nurse">Nurse</option>
<option value="other">Other</option>
</select><br />
<span id="webname">
<div class="form-group">
<%= form.label :speciality_id %>
<%= form.collection_select :speciality_id, Speciality.all, :id, :name, { include_blank: true }, class: "form-control" %>
</div>
</span>
<script>
document.getElementById("clinician_type").onchange = function () {
if (document.getElementById("clinician_type").options[document.getElementById("clinician_type").selectedIndex].value == "physician")
document.getElementById("webname").style.display = "block";
else
document.getElementById("webname").style.display = "none";
}
</script>
<span id="webname2">
<div class="form-group">
<%= form.label :occupation_id %>
<%= form.collection_select :occupation_id, Occupation.all, :id, :name, { include_blank: true }, class: "form-control" %>
</div>
</span>
<script>
document.getElementById("clinician_type").onchange = function () {
if (document.getElementById("clinician_type").options[document.getElementById("clinician_type").selectedIndex].value == "nurse")
document.getElementById("webname2").style.display = "block";
else
document.getElementById("webname2").style.display = "none";
}
</script>
后面还有更多选项,这些选项对于 physicians
和 nurses
都是相同的。
目前 Speciality
出现在所有选项中,Occupation
仅在 nurse
被 select 编辑(正如它应该的那样)时出现。
对于制作根据下拉菜单进行调整的表单的任何建议,我将不胜感激 selection。我已经尝试实现 17525724 的答案,但也没有让它起作用。
只需参考这个 railscast episode #88 他们已经从头开始构建了动态 select 菜单!
或者最好在 Youtube 上观看这个 revised #88 episode。
关注steps.You即可获得。
我目前有一个创建 clinicians
的表单。在这个表格上,我想有一个 clinician_type
的下拉列表,用户可以在 physician
、nurse
和 other
之间 select。根据用户的 selection,表单将适应允许他们 select a Speciality
for a physician
或 Occupation
for a nurse
.
我试图实现 23459948 的答案并使其适应具有三个选项,但我不知道 javascript 并且我无法让它工作。
Clinician
belongs_to
:occupation
和 :speciality
和 Occupation
和 Speciality
每个 has_many
:clinicians
。
我在表单中实现此功能的代码如下所示:
<%= form_for @clinician do |form| %>
Type of new clinician: <select id="clinician_type">
<option value="physician">Physician</option>
<option value="nurse">Nurse</option>
<option value="other">Other</option>
</select><br />
<span id="webname">
<div class="form-group">
<%= form.label :speciality_id %>
<%= form.collection_select :speciality_id, Speciality.all, :id, :name, { include_blank: true }, class: "form-control" %>
</div>
</span>
<script>
document.getElementById("clinician_type").onchange = function () {
if (document.getElementById("clinician_type").options[document.getElementById("clinician_type").selectedIndex].value == "physician")
document.getElementById("webname").style.display = "block";
else
document.getElementById("webname").style.display = "none";
}
</script>
<span id="webname2">
<div class="form-group">
<%= form.label :occupation_id %>
<%= form.collection_select :occupation_id, Occupation.all, :id, :name, { include_blank: true }, class: "form-control" %>
</div>
</span>
<script>
document.getElementById("clinician_type").onchange = function () {
if (document.getElementById("clinician_type").options[document.getElementById("clinician_type").selectedIndex].value == "nurse")
document.getElementById("webname2").style.display = "block";
else
document.getElementById("webname2").style.display = "none";
}
</script>
后面还有更多选项,这些选项对于 physicians
和 nurses
都是相同的。
目前 Speciality
出现在所有选项中,Occupation
仅在 nurse
被 select 编辑(正如它应该的那样)时出现。
对于制作根据下拉菜单进行调整的表单的任何建议,我将不胜感激 selection。我已经尝试实现 17525724 的答案,但也没有让它起作用。
只需参考这个 railscast episode #88 他们已经从头开始构建了动态 select 菜单!
或者最好在 Youtube 上观看这个 revised #88 episode。
关注steps.You即可获得。