2012-12-07 97 views
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> 

有没有什么解决方案来解决呢?
非常感谢帮助我。

回答

1

您正在选择div而不是输入。

$("#combine_category_id select").change(...

+0

我对jquery了解不多。在那种情况下,我应该改变什么? – Anonymous

+0

您需要更改的部分是我上面发布的部分。在这种情况下,添加'input'到你的jQuery选择器('$(“#combine_category_id”)') – Kevin

+1

不能用'$(“#combine_category_id input”)。change(function(){'and should i changed这个''div class =“field”id =“combine_category_id”>'? – Anonymous