javascript - 检测上传对话框中的打开按钮

标签 javascript jquery file-upload

有没有办法检测是否使用 JavaScript 或查询单击了上传对话框中的打开按钮? this button

更新: 我将向您展示我的示例代码:

<input type = "file" name = "imgUpload" id = "trngImgUpload" style = "display : none;" multiple>
<button class="btnTools" onclick = "trngOpenVidUploadDialog()" ><img src ="@Url.Content("~/Images/video_icon.jpg")" class ="img3"/></button>
<script>
function trngOpenVidUploadDialog() {
        //$('#modalMatchingTypePairType').modal('hide');
        //console.log(123);
        $('#trngImgUpload').trigger("click");

    };
    $('input[type=file]').change(function () {
        if (this.files) {
            alert('changed');
            var path = sendVidFile($('#trngImgUpload')[0].files[0], 1);
            $('#page' + currId).append('<div class = "vidDR" id = "tTool' + (toolIdCounter) + '"><div class = "del"></div><embed autostart="false" class = "vid" src="' + path + '"></embed></div>');
            $('.vidDR').resizable({
                containment: '#page' + currId
            }).draggable({
                containment: '#page' + currId
            });
            $(".vidDR").click(function () {
                alert(1);
            });
            $('#trngImgUpload').val("");
            $('#tTool' + toolIdCounter).contextmenu(function () {
                $(this).remove();
            });
            toolIdCounter++;
        }
    });
</script>

我已经尝试使用更改事件,但没有任何反应。我不知道我的代码有什么问题。

最佳答案

您无法捕获“打开”按钮上的操作,但是您可以捕获文件输入发生更改时的操作。例如。当用户使用文件输入选择一个新文件时。

示例如下:

// input on change event listener
$('#file-upload').on('change', function() {
  alert('File Attached: ' + $('#file-upload').val());
});
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- file upload input -->
<input type="file" id="file-upload">

希望这有帮助。

关于javascript - 检测上传对话框中的打开按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36257934/

相关文章:

json - 将文件和相关数据以 JSON 形式发布到 RESTful WebService

javascript - 如何在ajax javascript的foreach循环中通过日期时间转换数据的值。

jquery - 动态检查 SELECT 重复值

jquery - HTML5 文件上传需要 "mouseover with file"事件

javascript - 在表行中查找 td 元素位置

jquery - 如何使用 jQuery 垂直切换()内容?

javascript - 取消输入类型 ="file"上的事件隐藏

javascript - 在javascript中创建一个对象数组,然后获取属性

javascript - 使用 XSS 执行 PHP 代码

javascript - 使用立即需要该状态进行 API 调用的 useState Hook 时,如何等待 setState 调用完成?