wordpress - 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
有人可以让我知道我错过了什么。
提前致谢
解决方案
推荐阅读
- ios - Xcode项目添加视频文件,使用后删除
- javascript - 使用 Javascript 确定 Accept-Language HTTP 请求标头 - 或以其他方式确定用户的首选语言
- c# - Visual Studio 2017 命名参数语法突出显示?
- arrays - 如何比较一个数组中的对象
- mule - 如何检查有效载荷内的空数组?
- python - namedtuple 和 NamedTuple 有什么区别?
- java - 如何在 Gradle 插件中检查/读取 Java 文件
- python - Python HTTPSConnection 异常
- c# - 按钮循环 ASP.NET
- javascript - 在向 li 添加新课程之前删除课程