【问题标题】:How to show hide a text field when something is selected from a drop down.从下拉列表中选择某些内容时如何显示隐藏文本字段。
【发布时间】:2015-03-17 17:05:26
【问题描述】:

我的问题是,如果我在选择 ABS 时在下拉菜单中选择了一些内容,例如 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;'

【问题讨论】:

    标签: javascript jquery jquery-ui ruby-on-rails-4 simple-form


    【解决方案1】:

    如果这是您的 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();
            }
          });
        });
    

    【讨论】:

    • 我忘了那些。但是仍然没有运气让这个工作@BenJaspers
    • 文本框是否从未显示?我怀疑这个:input_html => {:value => ''} 可能会将所有选项的值设置为空字符串。
    • 你要发布你是如何修复它的吗??
    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多