javascript 用户上传文件时如何运行JS代码?

5f0d552i  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(124)

我希望在选择文件后弹出消息You uploaded 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>

如果你认为这篇文章可以改进,请考虑添加评论。

bqjvbblv

bqjvbblv1#

我设法用一个监听器解决了这个问题。

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

相关问题