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

前端 未结 28 1346
囚心锁ツ
囚心锁ツ 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 07:01

    My solution for Safari with jQuery and jQuery-ui:

    $("<input type='file' class='ui-helper-hidden-accessible' />").appendTo("body").focus().trigger('click');
    
    0 讨论(0)
  • 2020-11-21 07:01

    I was researching this a while ago because I wanted to create a custom button that would open the file dialog and start the upload immediately. I just noticed something that might make this possible - firefox seems to open the dialog when you click anywhere on the upload. So the following might do it:

    1. Create a file upload and a separate element containing an image that you want to use as the button
    2. Arrange them to overlap and make the file element backgroud and border transparent so the button is the only thing visible
    3. Add the javascript to make IE open the dialog when the button/file input is clicked
    4. Use an onchange event to submit the form when a file is selected

    This is only theoretical since I already used another method to solve the problem but it just might work.

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

    You can fire click() on any browser but some browsers need the element to be visible and focused. Here's a jQuery example:

    $('#input_element').show();
    $('#input_element').focus();
    $('#input_element').click();
    $('#input_element').hide();
    

    It works with the hide before the click() but I don't know if it works without calling the show method. Never tried this on Opera, I tested on IE/FF/Safari/Chrome and it works. I hope this will help.

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

    I have been searching for solution to this whole day. And these are the conclusions that I have made:

    1. For the security reasons Opera and Firefox don't allow to trigger file input.
    2. The only convenient alternative is to create a "hidden" file input (using opacity, not "hidden" or "display: none"!) and afterwards create the button "below" it. In this way the button is seen but on user click it actually activates the file input.

    Hope this helps! :)

    <div style="display: block; width: 100px; height: 20px; overflow: hidden;">
    <button style="width: 110px; height: 30px; position: relative; top: -5px; left: -5px;"><a href="javascript: void(0)">Upload File</a></button>
    <input type="file" id="upload_input" name="upload" style="font-size: 50px; width: 120px; opacity: 0; filter:alpha(opacity=0);  position: relative; top: -40px;; left: -20px" />
    </div>
    
    0 讨论(0)
  • 2020-11-21 07:06

    You cannot do that in all browsers, supposedly IE does allow it, but Mozilla and Opera do not.

    When you compose a message in GMail, the 'attach files' feature is implemented one way for IE and any browser that supports this, and then implemented another way for Firefox and those browsers that do not.

    I don't know why you cannot do it, but one thing that is a security risk, and which you are not allowed to do in any browser, is programmatically set the file name on the HTML File element.

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

    Hopefully this helps someone - I spent 2 hours banging my head against it:

    In IE8 or IE9, if you trigger opening a file input with javascript in any way at all (believe me I've tried them all), it won't let you submit the form using javascript, it will just silently fail.

    Submitting the form via a regular submit button may work but calling form.submit(); will silently fail.

    I had to resort to overlaying my select file button with a transparent file input which works.

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