简体   繁体   中英

Why is my Javascript alert not working on this custom WordPress plugin?

So I am finishing this custom WordPress plugin. Earlier I tested it to ensure that javascript was loading, but now when I test the subscription functionality, it subscribes to nothing, but I created an alert that tells me its working, except that its not.

This is my newsletter-subscriber/js/main.js file:

jQuery(document).ready(function($){
     $('#subscriber-form').submit(function(e){
        e.preventDefault();

        alert('Submitted');
     });
});

This is my newsletter-subscriber/includes/newsletter-subscriber-scripts.php file:

<?php

// Add Scripts
function ns_add_scripts(){
        wp_enqueue_style('ns-main-style', plugins_url().'/newsletter-subscriber/css/style.css');
        wp_enqueue_script('ns-main-script', plugins_url().'/newsletter-subscriber/js/main.js', array('jquery'));
}

add_action('wp_enqueue_scripts', 'ns_add_scripts');

Your code seems fine. A few things to check.

First check if your javascript is even loaded by checking the html in your browser.
Second check if the url is correct. Third go the JS file url, is the correct JS file loaded?

Is the plugin active? Does the CSS work?

Check the network tab of your dev tools. Do you see the js loaded there? Is the console giving errors. Is Jquery loaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM