在一個簡單的形式:顯示/隱藏文本字段
<%= 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