简体   繁体   中英

How do I run JS code when a user uploads files?

I want the message You uploaded a file! to pop up after I select a file.

<html>
    <head>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#uploadme").click(function() {
                    alert("You uploaded a file!")
                });
            });
        </script>
    </head>
    <body>
        <input type="file" id="uploadme"/>
    </body>
</html>

Please consider adding a comment if you think this post can be improved.

I managed to solve this problem using a listener.

<input type="file" id="uploadme">
<script>
  const fileSelector = document.getElementById('uploadme');
  fileSelector.addEventListener('change', (event) => {
    alert('You uploaded a file!')
  });
</script>

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