1
我正在做一個需要JQuery的表單,當組合框選擇1值時,標籤和文本框將顯示或隱藏取決於情況。當我嘗試在調試時使用googlechrome時,它運行正常。但是,當我嘗試在我的本地主機,它不起作用。
這是我的JQuery
JQuery隱藏和顯示功能RoR
<script type="text/javascript">
$(document).ready(function(){
$("#combine_category_id").change(function(){
if ($(this).val() == 1){
$("#type").hide();
$("#block").show();
$("#road").show();
$("#level").show();
$("#facing").show();
$("#size").show();
$("#value").hide();
$("#asking").show();
$("#project").show();
$("#unit").hide();
$("#match").show();
}
else if ($(this).val() == 2){
$("#type").hide();
$("#block").hide();
$("#road").show();
$("#level").hide();
$("#facing").show();
$("#size").show();
$("#value").hide();
$("#asking").show();
$("#project").hide();
$("#unit").show();
$("#match").show();
}
else if ($(this).val() == 3){
$("#type").show();
$("#block").show();
$("#road").show();
$("#level").show();
$("#facing").show();
$("#size").show();
$("#value").show();
$("#asking").show();
$("#project").hide();
$("#unit").hide();
$("#match").hide();
}
});
});
</script>
這是_form.html.rb在Ruby on Rails的
<div class="field" id="combine_category_id">
<%= f.label :category_id %><br />
<%= f.collection_select(:category_id , @get_master, :id , :category) %>
</div>
<div class="field" id="type">
<%= f.label :type %><br />
<%= f.number_field :type %>
</div>
<div class="field" id="project">
<%= f.label :project_name %><br />
<%= f.text_field :project_name %>
</div>
<div class="field" id="no">
<%= f.label :unit_no %><br />
<%= f.number_field :unit_no %>
</div>
<div class="field" id="block">
<%= f.label :block_no %><br />
<%= f.text_field :block_no %>
</div>
<div class="field" id="road">
<%= f.label :road_name %><br />
<%= f.text_field :road_name %>
</div>
<div class="field" id="level">
<%= f.label :level %><br />
<%= f.number_field :level %>
</div>
<div class="field" id="facing">
<%= f.label :facing %><br />
<%= f.text_field :facing %>
</div>
<div class="field" id="size">
<%= f.label :size %><br />
<%= f.number_field :size %>
</div>
<div class="field" id="value">
<%= f.label :value %><br />
<%= f.number_field :value %>
</div>
<div class="field" id="match">
<%= f.label :match_bank %><br />
<%= f.number_field :match_bank %>
</div>
<div class="field" id="asking">
<%= f.label :asking %><br />
<%= f.number_field :asking %>
</div>
<div class="actions">
<%= f.submit %>
</div>
有沒有什麼解決方案來解決呢?
非常感謝幫助我。
我對jquery瞭解不多。在那種情況下,我應該改變什麼? – Anonymous
您需要更改的部分是我上面發佈的部分。在這種情況下,添加'input'到你的jQuery選擇器('$(「#combine_category_id」)') – Kevin
不能用'$(「#combine_category_id input」)。change(function(){'and should i changed這個''div class =「field」id =「combine_category_id」>'? – Anonymous