从下拉列表中选择内容时如何显示隐藏文本字段。

How to show hide a text field when something is selected from a drop down.

我的问题是,如果我 select 在 ABS selected 时我的下拉菜单中有像 ABS 这样的东西,我怎样才能显示一个文本框来输入额外的信息。我现在所拥有的不起作用...非常感谢任何帮助!

这是我的app.js

$(document).ready(function(){

$('#indirect_id').change(function() {
    var indirect_id = $(this).val();
    if(indirect_id == 'ABS'){
      $('#sick_comment').show();
    }
    else{
      $('#sick_comment').hide();
    }
  });

这是我的看法

.row-fluid
  =simple_form_for @entry, :url => url_for(:controller => 'entry', :action => 'create'), :method => :post do |f|

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
  %th.lt Indirect Code:
  %td.lt= f.input_field :indirect_id, :as => :select, :label => false, :collection => ['PD', 'VAC', 'ABS'], :id => 'indirect_id', :input_html => {:value => ''}


  %th.lt Optional Comment
  %td.lt= f.text_field :sick_day,  :label => false, :id => 'sick_comment', :input_html => {:value => ''}

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
= f.button :submit, "Submit", :class => 'btn btn-primary', :style => 'margin-left:50px;'

如果那是您的 app.js 的全文,那么您缺少结束括号和圆括号:

 $(document).ready(function(){
    $('#indirect_id').change(function() {
        var indirect_id = $(this).val();
        if(indirect_id == 'ABS'){
          $('#sick_comment').show();
        }
        else{
          $('#sick_comment').hide();
        }
      });
    });