0

我正在尝试在我的 Wordpress 网站上添加一个脚本。使用 jQuery,我想将“onChange”事件添加到数字输入(属于计算字段表单插件)。

当我在输入字段中输入内容时,什么也没有发生。它应该已经打印onChange在控制台中。

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

HTML:

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

函数.php 文件:

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 的最后一个参数);
  • usingvar j = jQuery.noConflict();和 usingj代替$.

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

4

2 回答 2

0

利用:

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

别名$不绑定避免冲突

于 2021-01-19T17:14:47.020 回答
0

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

于 2021-01-19T18:11:49.267 回答