多条件控制器

时间:2020-02-13 作者:user2107656

我正在尝试在自定义程序中设置条件显示项目的多个实例。此代码适用于conditional\\u display()函数的任何一个实例,但不会同时执行这三个实例

(function() {


function conditional_display(conditional, condition_toggled) {
    wp.customize.control(conditional, function(control) {
        //conditions are the value(s) for controller that will trigger toggling
        conditions = condition_toggled.split(\',\');
        toggleControl = function toggleControl(value) {
            conditions.forEach(function(condition) {
                //this will get the condition that will result in display
                //and controllers of displayed items
                cond_display_control = condition.split(\'|\');
                //separates out the controllers to be displayed
                controllers = cond_display_control[1].split(\'^\');
                if (value == cond_display_control[0]) {

                    controllers.forEach(function(controller) {
                        wp.customize.control(controller).toggle(true);
                    });
                } else {
                    controllers.forEach(function(controller) {
                        wp.customize.control(controller).toggle(false);
                    });
                }
            });
        };

        toggleControl(control.setting.get());
        control.setting.bind(toggleControl);
    });
}
/**
 * Run functions when customizer is ready.
 */
wp.customize.bind(\'ready\', function() {
    conditional_display(\'jma_gbs_site_border_control\', \'yes|jma_gbs_site_border_color_control^jma_gbs_site_border_width_control^jma_gbs_site_border_radius_control\');

    conditional_display(\'jma_gbs_modular_header_control\', \'yes|jma_gbs_header_border_color_control^jma_gbs_header_border_width_control^jma_gbs_header_border_radius_control\');

    conditional_display(\'jma_gbs_modular_footer_control\', \'yes|jma_gbs_footer_border_color_control^jma_gbs_footer_border_width_control^jma_gbs_footer_border_radius_control\');
});
})();
有什么想法吗?

1 个回复
最合适的回答,由SO网友:Tim Elsass 整理而成

是-看起来您打算创建的变量只是在每次调用中重新分配。您应该使用var 每次之前:

        var conditions, toggleControl; // here.

        conditions = condition_toggled.split(\',\');
        toggleControl = function toggleControl(value) {
            conditions.forEach(function(condition) {
                var cond_display_control, controllers; // here.

                //this will get the condition that will result in display
                //and controllers of displayed items
                cond_display_control = condition.split(\'|\');
                //separates out the controllers to be displayed
                controllers = cond_display_control[1].split(\'^\');
                if (value == cond_display_control[0]) {

                    controllers.forEach(function(controller) {
                        wp.customize.control(controller).toggle(true);
                    });
                } else {
                    controllers.forEach(function(controller) {
                        wp.customize.control(controller).toggle(false);
                    });
                }
            });
        };

相关推荐

主题定制--当使用php变量时,如何在外部存储javascript?

EDIT: Complete re-write在过去的几周里,我一直在为我的主题开发一个自定义扩展。这是我第一次用Wordpress“开发”。所有的工作都很好,我对结果的功能很满意,尽管它需要一些整理。我希望将我的js函数移到一个外部文件-自定义。js。原始格式:main-block.php$custom1 = $custom_meta[\'custom1\']; <form name=\"customForm\"> Validatio