Detecting if the file input dialog is open

前端 未结 4 1524
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 00:24

How can I go about detecting if the file input dialog is currently open?

I\'m trying to integrate some file upload functionality into a popup (bootstrap style) model

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 01:10

    In my case works this code on jQuery:

    // esc must close popup
    $("body").keyup(function(e) {
        if (27 == e.keyCode) {
            hidepopup();
        }
    });
    
    // input in popup
    var $file = $("input:file");
    // keyup will be catched for input, not for body
    $file.bind("click", function(e) {
        $(this).focus();
    });
    // keyup catching will be changed back to body after selecting file
    $file.bind("change", function(e) {
        $(this).blur();
    });
    // we catched esc keyup, so change esc catching back to the body
    $file.keyup(function(e) {
        if (27 == e.keyCode) {
            $(this).blur();
            // i don't know, why it works with return false, because i am not js programmer ), but cancelBubble or e.preventDefault is not working
            return false;
        }
    });
    

提交回复
热议问题