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

标签 jquery

我有一个在 .change() 上运行的 jquery 代码。 但我想在 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 上运行上面的代码?

最佳答案

只需调用 .change()没有争论。将整个内容放入准备好的处理程序中,然后:

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

关于jQuery - 运行change()和ready()函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247279/

相关文章:

javascript - 防止用户更改单选按钮的状态

javascript - 将插入符号移动到焦点文本区域的末尾

javascript - 如何使用数组和 JQuery 创建可过滤菜单?

javascript - Socket.io node.js,如何记录连接时间避免或考虑页面刷新/多个套接字?

javascript - 我如何总结值并使用 jquery 自动显示它们

javascript - 查找具有特定数据属性 jquery 的最近的前一个元素

javascript - 具有顶部导航和左侧导航的自定义母版页

javascript - jQuery 逗号分隔字符串到对象映射

javascript - 在 Rails 应用程序中组织 javascript

javascript - 如何从 JavaScript 向 Rails 发送带参数的 http 请求