jQuery函数未在Wordpress中注册



我想在我的Wordpress网站上添加一个脚本。使用jQuery,我想添加一个"onChange"事件转换为数字输入(属于计算字段表单插件)。

当我在输入字段中输入一些东西时,什么也没有发生。它应该在控制台中打印onChange

所以问题是jQuery函数没有注册。

HTML:

<input id="fieldname2_1" name="fieldname2_1" min="1" type="number">

显然也文件:

function numeric_input_script() {
wp_enqueue_script(
'numeric-input', // naming my script so that i can attach other scripts and de-register, etc.
get_stylesheet_directory_uri() . '/numeric-input.js', // this is the location of the script file
array('jquery'), // this array lists the scripts upon which my script depends
null, // version number
true // states that the script needs to be loaded in the footer
);
}
add_action( 'wp_enqueue_scripts', 'numeric_input_script' );

JS文件:

jQuery(document).ready(function($) {
console.log('Loading'); //shows up
$('#fieldname2_1').on('change', function() {
console.log("onChange"); //code inside this function does not work
})
console.log('Loaded');  //shows up
});

我尝试过但没有成功的事情:

  • 按钮和其他事件;
  • 在标题之后排队脚本(wp_enqueue_script的最后一个参数设为false);
  • 使用var j = jQuery.noConflict();和使用j代替$

任何帮助将不胜感激。谢谢!

使用说明:

jQuery('#fieldname2_1').on('change', function() {
console.log("onChange"); //code inside this function does not work
})

不绑定$别名以避免冲突

函数已注册,但实际问题是由计算字段表单插件引起的。