我正在尝试从 wp 插件加载 .js 文件。
我加载 jquery、jquery-UI 和 .js 文件的代码如下所示,位于“主”插件文件中:
//Load Java and Jquery
function load_jquery() {
    // only use this method is we're not in wp-admin
    if (!is_admin()) {
        // deregister the original version of jQuery
        wp_deregister_script('jquery');
        wp_deregister_script('jquery-ui');
        wp_deregister_script('lyox-script');
        // discover the correct protocol to use
        $protocol='http:';
        if($_SERVER['HTTPS']=='on') {
            $protocol='https:';
        }
        // register the Google CDN version
        wp_register_script('jquery', $protocol.'//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2');
        wp_register_script('jquery-ui', $protocol.'//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', false, '1.10.3');
        wp_register_script('lyox-script', plugins_url( '/includes/script.js' , __FILE__ ), array( 'jquery', 'jquery-ui' ) );
        // add it back into the queue
        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-ui');
        wp_enqueue_script('lyox-script');
    }
}
add_action('template_redirect', 'load_jquery');
然后在 .js 文件中,我有以下代码,其中 post() 函数被添加到表单按钮 onclick="post();" 中:
$(document).ready(function() {
        function post() {
        var name = $('#name').val();
             $.post('process.php', {postname:name},
                    function(data)
                        {
                        alert(data);
                        $('#result').html(data);                        
             });
    }
});
当我在页面上尝试时仍然没有任何反应。有任何想法吗?