jQuery - 运行change()和ready()的函数

use*_*007 13 jquery

我有一个运行的jquery代码.change().但我想在jquery上运行相同的代码.ready(),但它不起作用.

这是我的代码:

    jQuery('.nhp-opts-select-hide-below').change(function(){
        var option = jQuery('option:selected', this);
        if(option.data('show').length > 0 || option.data('hide').length > 0){
            jQuery(option.data('show')).each(function(){
                if(jQuery(this).closest('tr').is(':hidden')){
                    jQuery(this).closest('tr').fadeIn('fast');
                }
            });
            jQuery(option.data('hide')).each(function(){
                if(jQuery(this).closest('tr').is(':visible')){
                    jQuery(this).closest('tr').fadeOut('fast');
                }
            });

        }else{
            jQuery(option.data('show')).each(function(){
                if(jQuery(this).closest('tr').is(':visible')){
                    jQuery(this).closest('tr').fadeOut('fast');
                }
            });
            jQuery(option.data('hide')).each(function(){
                if(jQuery(this).closest('tr').is(':hidden')){
                    jQuery(this).closest('tr').fadeIn('fast');
                }
            });     
        }
    }); 
Run Code Online (Sandbox Code Playgroud)

请告诉我如何在jquery准备好运行上面的代码?

Mat*_*all 20

只是打电话.change()没有参数.把整个东西放在你准备好的处理程序中,然后:

jQuery(function($) {
    $('.nhp-opts-select-hide-below').change(function(){
        // snip...
    }).change(); // that's it
});
Run Code Online (Sandbox Code Playgroud)