Javascript jQuery隐藏/显示,警报第一次不起作用,但第二次就可以完美地工作

Javascript jQuery隐藏/显示,警报第一次不起作用,但第二次就可以完美地工作,javascript,jquery,ruby-on-rails,forms,Javascript,Jquery,Ruby On Rails,Forms,我的页面在开始时应该是这样的 当我选中复选框时,文本框应该出现,否则它应该被隐藏 我在html的开头加载javascript 我的javascript看起来像这样 $(document).ready(function(){ if($('#user_tech_know').is(':checked')) $('#user_tech_areas').show(); else $('#user_tech_areas').hide();

我的页面在开始时应该是这样的

当我选中复选框时,文本框应该出现,否则它应该被隐藏

我在html的开头加载javascript

我的javascript看起来像这样

 $(document).ready(function(){

    if($('#user_tech_know').is(':checked')) 
        $('#user_tech_areas').show();
    else 
        $('#user_tech_areas').hide();

    if($('#user_other').is(':checked')) 
        $('#user_other_areas').show();
    else 
        $('#user_other_areas').hide();


$(":checkbox").change(function(){
    if($('#user_tech_know').is(':checked')) 
        $('#user_tech_areas').show();
    else 
        $('#user_tech_areas').hide();

    if($('#user_other').is(':checked')) 
        $('#user_other_areas').show();
    else 
        $('#user_other_areas').hide();

    if(($(":checkbox:checked").length)> 3)
alert('Sorry, maximum 3 boxes may be checked.');


});


if(($(":checkbox:checked").length)> 3)
alert('Sorry, maximum 3 boxes may be checked.');
     });
隐藏/显示和警报第一次不起作用。但从第二次开始就完美地工作了

不知道我哪里出错了。是因为document.readyfunction吗

我的HTML看起来像这样

   %script(src ="/app/assets/a.js")
   %h1 Mentee Registration

  .row
   .col-md-6.col-md-offset-3
     = form_for(@user) do |f|
       = render 'error_messages'
       = f.label :'Mentee Information'
       = f.label :'Cisco Email'
       = f.email_field :cisco_email
       = f.label :'Current group'
       = f.text_field :current_group
       = f.label :'Current work location,city'
       = f.select :work_city, options_for_select(%w[Bangalore-India Chennai-India Hyderabad-India SanJose/Milpitas SanFrancisco Ottawa RTP Switzerland]), include_blank: true
       = f.label :'Developmental goals ,  check at least one box that apply, can select max 3 choices from the list below'
       = f.check_box :conflict_resolution
       = f.label :conflict_resolution, 'Conflict Resolution'      
       = f.check_box :customer_know_how
       = f.label :customer_know_how, 'Customer Know How'
       = f.check_box :exec_acheive_results
       = f.label :exec_acheive_results, 'Executive to achieve results'
       = f.check_box :personal_branding
       = f.label :personal_branding, 'Personal Branding'
       = f.check_box :leading_change 
       = f.label :leading_change, 'Leading Change'
       = f.check_box :align_and_influence    
       = f.label :align_and_influence, 'Align and Influence'
       = f.check_box :managing_without_authority
       = f.label :managing_without_authority, 'Managing Without Authority'
       = f.check_box :win_win_negotiation
       = f.label :win_win_negotiation, 'Win-Win Negotiation'
       = f.check_box :career_exploration
       = f.label :career_exploration, 'Career Exploration'
       = f.check_box :effective_communication
       = f.label :effective_communication, 'Effective Communication'
       = f.check_box :think_out_box
       = f.label :think_out_box, 'Creative Thinking/Think Out Of the box'
       = f.check_box :tech_know
       = f.label :tech_know, 'Technical Know-How, List Areas'
       = f.text_field :tech_areas
       = f.check_box :other
       = f.label :other, 'Any Other'      
       = f.text_field :other_areas
       = f.submit "Register Me", class: "btn btn-primary"

添加警报$'user\u tech\u know'。在dom ready Handler中的if条件之前添加一个长度。您能为它添加一个吗?我只是尝试隐藏/显示文本框,并在复选框数超过3时显示一条警报消息。我认为加上那句话不会有帮助@ArunPJohnyI添加了屏幕截图。有帮助吗@Manoz@Novice截图没有多大帮助。。。您需要共享html