JQuery在来自代码段或文件的页面上不起作用

时间:2021-11-11 作者:RawalD

I\'ve been banging my head against the wall on this, My jQuery code runs perfect in CodePen, the WP site in console also runs it perfect, however I simply can not get it to run on the actual WooCommerce My Account Billing Address page no matter what.

My jQuery:

jQuery(document).ready(function () {
    // Your code in here
    jQuery(\'#billing_test\').on(\'change\', function () {
        myFunc();
    })
    function myFunc() {
        // your function code
        var complex_name = jQuery(\'#billing_test\').val();

        var suburb = jQuery(\'#billing_suburb\').val();

        if (complex_name == \'mr\') {
            alert(\'works\');
            jQuery("#billing_suburb").val(\'LDM\');
            jQuery(\'#billing_postcode\').val(\'5000\');
        }
    }
})

I\'ve tried adding via Code Snippets to no luck so tried a script file with the following code:

function my_theme_scripts() {
    wp_enqueue_script( \'test\', get_template_directory_uri() . \'/js/test.js\', array( \'jquery\' ) );
}
add_action( \'wp_enqueue_scripts\', \'my_theme_scripts\' );

But still nothing. Any and all help will be really grateful as I\'ve attempted everything I could but still nothing.

I have also tried replacing the $ with jQuery, tried the $.noConflict() as well.

Added script manually and it is in the inspect as well.

HTML

<p class="form-row my-css" id="billing_test_field" data-priority=""><label for="billing_test" class="">Test</label><span
        class="woocommerce-input-wrapper"><select name="billing_test" id="billing_test" class="select "
            data-allow_clear="true" data-placeholder="Title">
            <option value="">Please select</option>
            <option value="mr">Mr</option>
            <option value="ms" selected=\'selected\'>Ms</option>
        </select></span></p>

How do I not use if and else if to match the conditions:

if (complex_name == \'eldogleneast\') {
   jQuery("#billing_suburb").val(\'ELD\');
   jQuery(\'#billing_postcode\').val(\'0157\');
}
    else if (complex_name == \'eldoglen\'){
                jQuery("#billing_suburb").val(\'ELD\');
   jQuery(\'#billing_postcode\').val(\'0157\');
             }
1 个回复
SO网友:RawalD

该文件是从父主题目录而不是子主题目录读取的,因此该文件的问题是无法读取。

从“网络”选项卡跟踪目录中找到它

相关推荐

JQuery Datepicker在与日历交互之前显示错误的语言

在使用Woody Snippets插件编写为PHP代码段的完全自定义页面中,我需要使用jQuery日期选择器来选择日期。我正确地导入了必要的文件,并正确地调用和设置了日期选择器。昨天,我将日期选择器连接到一个隐藏的输入字段,这样当我单击一个图标时,日历就会浮动到位,一切都很好,即使是本地化,尽管我没有采取任何措施来本地化日历(导入i18n文件等)。。。今天,我不得不稍微改变一下页面的布局,所以我必须从头开始(内联)显示日历。根据demo 页面中,为了将日期选择器内联,只需将其附加到div而不是输入字段。d