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

前端 未结 28 1384
囚心锁ツ
囚心锁ツ 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:52

    Here is pure JavaScript solution to this problem. Works well across all browsers

    <script>
        function upload_image_init(){
            var elem = document.getElementById('file');
            if(elem && document.createEvent) {
               var evt = document.createEvent("MouseEvents");
               evt.initEvent("click", true, false);
               elem.dispatchEvent(evt);
            }
        }
    </script>
    
    0 讨论(0)
  • 2020-11-21 06:52

    There are ways to redirect events to the control but don't expect to be able to easily fire events to the fire control yourself as the browsers will try to block that for (good) security reasons.

    If you only need the file dialog to show up when a user clicks something, let's say because you want better looking file upload buttons, then you might want to take a look at what Shaun Inman came up with.

    I've been able to achieve keyboard triggering with creative shifting of focus in and out of the control between keydown, keypress & keyup events. YMMV.

    My sincere advice is to leave this the alone, because this is a world of browser-incompatibility-pain. Minor browser updates may also block tricks without warning and you may have to keep reinventing hacks to keep it working.

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

    it's not impossible:

    var evObj = document.createEvent('MouseEvents');
    evObj.initMouseEvent('click', true, true, window);  
    setTimeout(function(){ document.getElementById('input_field_id').dispatchEvent(evObj); },100);
    

    But somehow it works only if this is in a function which was called via a click-event.

    So you might have following setup:

    html:

    <div onclick="openFileChooser()" class="some_fancy_stuff">Click here to open image chooser</div>
    <input type="file" id="input_img">
    

    JavaScript:

        function openFileChooser() {
          var evObj = document.createEvent('MouseEvents');
          evObj.initMouseEvent('click', true, true, window);  
          setTimeout(function()
          { 
            document.getElementById('input_img').dispatchEvent(evObj);      
          },100);      
        }
    
    0 讨论(0)
  • 2020-11-21 06:54

    I know this is old, and all these solutions are hacks around browser security precautions with real value.

    That said, as of today, fileInput.click() works in current Chrome (36.0.1985.125 m) and current Firefox ESR (24.7.0), but not in current IE (11.0.9600.17207). Overlaying a file field with opacity 0 on top of a button works, but I wanted a link element as the visible trigger, and hover underlining doesn't quite work in any browser. It flashes on then disappears, probably the browser thinking through whether hover styling actually applies or not.

    But I did find a solution that works in all those browsers. I won't claim to have tested every version of every browser, or that I know it'll continue to work forever, but it appears to meet my needs now.

    It's simple: Position the file input field offscreen (position: absolute; top: -5000px), put a label element around it, and trigger the click on the label, instead of the file field itself.

    Note that the link does need to be scripted to call the click method of the label, it doesn't do that automatically, like when you click on text inside a label element. Apparently the link element captures the click, and it doesn't make it through to the label.

    Note also that this doesn't provide a way to show the currently selected file, since the field is offscreen. I wanted to submit immediately when a file was selected, so that's not a problem for me, but you'll need a somewhat different approach if your situation is different.

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

    Here is solution that work for me: CSS:

    #uploadtruefield {
        left: 225px;
        opacity: 0;
        position: absolute;
        right: 0;
        top: 266px;
        opacity:0;
        -moz-opacity:0;
        filter:alpha(opacity:0);
        width: 270px;
        z-index: 2;
    }
    
    .uploadmask {
        background:url(../img/browse.gif) no-repeat 100% 50%;
    }
    #uploadmaskfield{
        width:132px;
    }
    

    HTML with "small" JQuery help:

    <div class="uploadmask">
        <input id="uploadmaskfield" type="text" name="uploadmaskfield">
    </div>
    <input id="uploadtruefield"  type="file" onchange="$('#uploadmaskfield').val(this.value)" >
    

    Just be sure that maskfied is covered compeltly by true upload field.

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

    You can use

    <button id="file">select file</button>
    <input type="file" name="file" id="file_input" style="display:none;">
    <script>
    $('#file').click(function() {
            $('#file_input').focus().trigger('click');
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题