In JavaScript can I make a “click” event fire programmatically for a file input element?

前端 未结 28 1385
囚心锁ツ
囚心锁ツ 2020-11-21 06:29

I\'d like to make a click event fire on an tag programmatically.

Just calling click() doesn\'t seem to do anything or at lea

相关标签:
28条回答
  • 2020-11-21 06:59

    I had a <input type="button"> tag hidden from view. What I did was attaching the "onClick" event to any visible component of any type such as a label. This was done using either Google Chrome's Developer Tools or Mozilla Firefox's Firebug using the right-click "edit HTML" command. In this event specify the following script or something similar:

    If you have JQuery:

    $('#id_of_component').click();
    

    if not:

    document.getElementById('id_of_component').click();
    

    Thanks.

    0 讨论(0)
  • 2020-11-21 06:59

    Hey this solution works. for download we should be using MSBLOB

    $scope.getSingleInvoicePDF = function(invoiceNumberEntity) {
       var fileName = invoiceNumberEntity + ".pdf";
       var pdfDownload = document.createElement("a");
       document.body.appendChild(pdfDownload);
    
       AngularWebService.getFileWithSuffix("ezbillpdfget",invoiceNumberEntity,"pdf" ).then(function(returnedJSON) {
           var fileBlob = new Blob([returnedJSON.data], {type: 'application/pdf'});
           if (navigator.appVersion.toString().indexOf('.NET') > 0) { // for IE browser
               window.navigator.msSaveBlob(fileBlob, fileName);
           } else { // for other browsers
               var fileURL = window.URL.createObjectURL(fileBlob);
               pdfDownload.href = fileURL;
               pdfDownload.download = fileName;
               pdfDownload.click();      
           }
       });
    };
    

    For AngularJS or even for normal javascript.

    0 讨论(0)
  • 2020-11-21 06:59

    You can do this as per answer from Open File Dialog box on <a> tag

    <input type="file" id="upload" name="upload" style="visibility: hidden; width: 1px;     height: 1px" multiple />
    <a href="" onclick="document.getElementById('upload').click(); return false">Upload</a>
    
    0 讨论(0)
  • 2020-11-21 07:00

    WORKING SOLUTION

    Let me add to this old post, a working solution I used to use that works in probably 80% or more of all browsers both new and old.

    The solution is complex yet simple. The first step is to make use of CSS and guise the input file type with "under-elements" that show through as it has an opacity of 0. The next step is to use JavaScript to update its label as needed.

    HTML The ID's are simply inserted if you wanted a quick way to access a specific element, the classes however, are a must as they relate to the CSS that sets this whole process up

    <div class="file-input wrapper">
        <input id="inpFile0" type="file" class="file-input control" />
        <div class="file-input content">
            <label id="inpFileOutput0" for="inpFileButton" class="file-input output">Click Here</label>
            <input id="inpFileButton0" type="button" class="file-input button" value="Select File" />
        </div>
    </div>
    

    CSS Keep in mind, coloring and font-styles and such are totally your preference, if you use this basic CSS, you can always use after-end mark up to style as you please, this is shown in the jsFiddle listed at the end.

    .file-test-area {
        border: 1px solid;
        margin: .5em;
        padding: 1em;
    }
    .file-input {
        cursor: pointer !important;
    }
    .file-input * {
        cursor: pointer !important;
        display: inline-block;
    }
    .file-input.wrapper {
        display: inline-block;
        font-size: 14px;
        height: auto;
        overflow: hidden;
        position: relative;
        width: auto;
    }
    .file-input.control {
        -moz-opacity:0 ;
        filter:alpha(opacity: 0);
        opacity: 0;
    
        height: 100%;
        position: absolute;
        text-align: right;
        width: 100%;
        z-index: 2;
    }
    .file-input.content {
        position: relative;
        top: 0px;
        left: 0px;
        z-index: 1;
    }
    .file-input.output {
        background-color: #FFC;
        font-size: .8em;
        padding: .2em .2em .2em .4em;
        text-align: center;
        width: 10em;
    }
    .file-input.button {
        border: none;
        font-weight: bold;
        margin-left: .25em;
        padding: 0 .25em;
    }
    

    JavaScript Pure and true, however, some OLDER (retired) browsers may still have trouble with it (like Netscrape 2!)

    var inp = document.getElementsByTagName('input');
    for (var i=0;i<inp.length;i++) {
        if (inp[i].type != 'file') continue;
        inp[i].relatedElement = inp[i].parentNode.getElementsByTagName('label')[0];
        inp[i].onchange /*= inp[i].onmouseout*/ = function () {
            this.relatedElement.innerHTML = this.value;
        };
    };
    

    Working jsFiddle Example

    0 讨论(0)
  • 2020-11-21 07:00

    This code works for me. Is this what you are trying to do?

    <input type="file" style="position:absolute;left:-999px;" id="fileinput" />
    <button  id="addfiles" >Add files</button>
    
    <script language="javascript" type="text/javascript">
       $("#addfiles").click(function(){
          $("#fileinput").click();
       });
    </script>
    
    0 讨论(0)
  • 2020-11-21 07:01

    If you want the click method to work on Chrome, Firefox, etc, apply the following style to your input file. It will be perfectly hidden, it's like you do a display: none;

    #fileInput {
        visibility: hidden;
        position: absolute;
        top: 0;
        left: -5000px;
    }
    

    It's that simple, I tested it works!

    0 讨论(0)
提交回复
热议问题