在一个简单的形式:显示/隐藏文本字段
<%= 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>
请出示输出HTML作为浏览器发现。 “集成”意味着什么? – charlietfl
通过'integrate',我的意思是 - 我在哪里放置JQuery代码,以及为了触发它我还需要做些什么。 – 2scottish