简体   繁体   中英

JavaScript code not working for external file

I have JavaScript code that send data to backend which when I place inline then work fine but when I place in external file it does not work. Meanwhile other JavaScript is working in external file.

<script>
$('#submit-ajax').click(function(event) {
    console.log('working or not')
    $.post(
        '/order/detail/',
        {
            'test': 'hi i am here',
        },
        function() {
            alert( "Data Loaded: " );
        }
    )
});
</script>

in external file code is:

$(document).ready(function(){
    $('#submit-ajax').click(function(event) {
        console.log('working or not')
        $.post(
            '/order/detail/',
            {
                'test': 'hi i am here',
            },
            function() {
                alert( "Data Loaded: " );
            }
        )
    });
}

I tried a simple example,

HTML code: Relative path of external JS you need to give

<!DOCTYPE html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

<script src="(path of external js)\refJS.js"></script>

</head>
<body>
<div id="submit-ajax">
            click me
        </div>
</body>

In JS external file :

$(document).ready(function(){
    $('#submit-ajax').click(function(event) {
        console.log('working or not');

    });
})

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