首页 > 解决方案 > Wordpress 插件 - onclick 中的 javascript 功能不起作用

问题描述

我正在使用 wordpress 插件。我需要使用一些 javascript 函数。所以我遵循了以下步骤。

这是我的构造函数

public function __construct() {
        $this->plugin = plugin_basename(__FILE__);
        $this->includes();
        $this->initHooks();
}

我在 initHooks 函数中包含了一些钩子

public function includes() {
    include sprintf( '%s/includes/package.php', dirname( __FILE__ ) . '/' );
    include sprintf( '%s/includes/booking.php', dirname( __FILE__ ) . '/' );
}

这是我的包含功能

public function initHooks() {
        add_action("admin_enqueue_scripts", array($this, "enqueScripts"));
        add_action("admin_menu", array($this, "adminMenuPages"));
        add_filter("plugin_action_links_$this->plugin", array($this, "settingsLink"));
}

这是我的钩子函数

public function enqueScripts() {
        wp_enqueue_style('az-styles', plugins_url("/assets/css/style.css", __FILE__));
        wp_enqueue_script('az-scripts', plugins_url("/assets/js/lib/jquery-3.6.0.min.js", __FILE__), array('jquery'));
        wp_enqueue_script('az-scripts', plugins_url("/assets/js/app.js", __FILE__), array('jquery'));
    }
}

这是我的脚本部分,javascript 已成功加载

function clickHandler() {
    var name = jQuery("#name").value()
    if(name == "") {
        alert("The name field is required")
    }
}

这是我的javascript函数

我在 package.php 文件中使用这个函数

// Output the field
        echo '<input type="text" id="name" onclick="clickHandler()" name="location" value="' . esc_textarea( $location )  . '" class="widefat">';
        echo '<input type="text" name="phone" value="' . esc_textarea( $phone )  . '" class="widefat">';
        echo '<input type="text" name="email" value="' . esc_textarea( $email )  . '" class="widefat">';
        echo '<input type="text" name="description" value="' . esc_textarea( $description )  . '" class="widefat">';
        echo '<input type="text" name="address" value="' . esc_textarea( $address )  . '" class="widefat">';

但我收到以下错误

Uncaught ReferenceError: clickHandler is not defined

有人可以让我知道我错过了什么。

提前致谢

标签: wordpress

解决方案


推荐阅读