以简单的形式:
<%= form_for @user do |f| %>
<%= f.label :source, "How did you find out about us?", :class => "control-label" %>
<%= f.select(:source, options_for_select([['-- Please select --',nil],['Source 1','Source 1'], ['Source 2','Source 2'], ['Other','Other']])) %>
<%= f.label :source_other, "Specify Other Source" %>
<%= f.text_field :source_other %>
<% end %>
我正在尝试学习如何使用 JQuery 仅在从“源”字段中选择值“其他”时显示“source_other”文本字段。从我在网上看到的,看来我需要使用这样的东西:
$("#source").change(function() {
if ($("#source").val()=="Other")
$("#source_other").show();
else
$("#source_other").hide();
});
但是,我不太了解如何将 JQuery 与我的表单集成。有人可以指出我正确的方向吗?
更新:
这是生成的 html 片段:
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
<label class="control-label" for="user_lead_source">How did you find out about us?</label>
<select id="user_source" name="user[source]"><option value="">-- Please select --</option>
<option value="Source 1">Source 1</option>
<option value="Source 2">Source 2</option>
<option value="Other">Other</option>
</select>
<label for="user_source_other">Specify Other Source</label>
<input id="user_source_other" name="user[source_other]" size="30" type="text" />
</form>