0

我有基本的网络表单,我需要根据下拉结果显示/隐藏一个字段,这是我目前拥有的:

 <% choose_me = "--Choose one--" %>
<script type="text/javascript"> 
var f_distributor;  

function show_hide_fields(type){
    if (show_hide_fields == "Retailer"){            
        f_distributor = new LiveValidation('i_distributor');
        f_distributor.add( Validate.Presence );         
        $('#distributor').show();
    }
    if (show_hide_fields == "Distributor"){
        f_distributor.destroy();            
        $('#distributor').hide();           
    }
    if (show_hide_fields == "Direct Purchase Retailer"){
        f_distributor.destroy();            
        $('#distributor').hide();
    }
    if (show_hide_fields == "Distributor Rep"){
        f_distributor.destroy();            
        $('#distributor').hide();
    }
    if (show_hide_fields == "Catalog/E-Commerce Company"){
        f_distributor.destroy();            
        $('#distributor').hide();
    }
    if (show_hide_fields == "Other"){
        f_distributor.destroy();            
        $('#distributor').hide();
    }
    if (show_hide_fields == "--Choose one--"){
        f_distributor.destroy();            
        $('#distributor').hide();
    }
}
$(document).ready(function(){

    f_distributor = new LiveValidation('i_distributor');
    var f_type = new LiveValidation('i_type');
    f_type.add( Validate.Exclusion, { within: ['<%= choose_me %>'], failureMessage: "You must choose from the list."} );
    var f_dealer_business_name = new LiveValidation('i_dealer_business_name');
    f_dealer_business_name.add( Validate.Presence );
    var f_dealer_address1 = new LiveValidation('i_dealer_address1');
    f_dealer_address1.add( Validate.Presence );
    var f_dealer_city = new LiveValidation('i_dealer_city');
    f_dealer_city.add( Validate.Presence );
    var f_dealer_state = new LiveValidation('i_dealer_state');
    f_dealer_state.add( Validate.Exclusion, { within: ['<%= choose_me %>'], failureMessage: "You must choose from the list."} );
    var f_dealer_zip = new LiveValidation('i_dealer_zip');
    f_dealer_zip.add( Validate.Presence );
    f_dealer_zip.add( Validate.Length, { minimum: 5 } )
    var f_phone = new LiveValidation('i_phone');
    f_phone.add( Validate.Presence );
    f_phone.add( Validate.Length, { minimum: 10 } )
    var f_user_name = new LiveValidation('i_user_name');
    f_user_name.add( Validate.Presence );
    f_user_name.add( Validate.Email )
    var f_password = new LiveValidation('i_password');
    f_password.add( Validate.Presence );
    f_password.add( Validate.Length, { minimum: 6 } );
    var f_terms_acceptance = new LiveValidation('i_terms_acceptance', { insertAfterWhatNode: 'terms_acceptance_error' });
    f_terms_acceptance.add ( Validate.Acceptance );
    show_hide_fields($("input[@name='i_account']:checked").val());
});

                <div class="field"><%= select_tag "i[type]", options_for_select([ choose_me, "Retailer", "Distributor", "Direct Purchase Retailer", "Distributor Rep", "Catalog/E-Commerce Company", "Other" ], choose_me), :onchange => "if(show_hide_fields.value =='Retailer'){$('#distributor').show()} else {$('#distributor').hide()}; return false" %></div>             

表单没有抛出任何错误,但也没有显示隐藏字段。我使用 id 来显示和隐藏该字段。任何帮助将非常感激。

4

0 回答 0