Wordpress:未捕获引用错误:未定义myfunction



这是我在wordpress中排队的javascript,但我有这个错误。"未捕获引用错误:未定义myfunction"。

(function($) {
    function myfunction(bf) {
        if(bf.checked)
            var text1 = document.getElementById("shipping_first_name").value;
        else
            text1='';
        document.getElementById("billing_first_name").value = text1;
        };
})(jQuery);

我也尝试过这个代码。

(function($) {
    jQuery(document).ready(function($) {
        function myFunction(bf) {
            if(bf.checked)
                var text1 = document.getElementById("shipping_first_name").value;
            else
                text1='';
            document.getElementById("billing_first_name").value = text1;
            }
    });
})(jQuery);

排队:

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_template_directory_uri() . '/js/shipping.js',
        array( 'jquery' ),
        false,
        '1.0',
        true
    );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

在闭包内部定义的方法只能在闭包本身内部访问。

            (function(){
                /* This is called closure
                 All code here is solely on clouser
                */
            }())

要访问该方法,您可以按照以下更改闭包

            (function(){
                window.myfunction = function(bf){
                    /*...*/
                }
            }())

最新更新