13

で実行される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');
                }
            });     
        }
    }); 

上記のコードをjquery Readyで実行する方法を教えてください??

4

2 に答える 2

20

.change()引数なしで呼び出すだけです。そのすべてを準備済みハンドラーの中に入れてから、次のようにします。

jQuery(function($) {
    $('.nhp-opts-select-hide-below').change(function(){
        // snip...
    }).change(); // that's it
});
于 2013-06-22T04:24:42.077 に答える
3

最も簡単な方法は、準備完了時に変更イベントを手動でトリガーすることです。

jQuery(function() {
    $('.nhp-opts-select-hide-below').change();
});
于 2013-06-22T04:25:19.910 に答える