最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger click on input file element by clicking on another element - Stack Overflow

programmeradmin3浏览0评论

I am using jQuery and Ajax on a button, to update the value of attribute "accept" on an input file upload element, then trigger click on it. But I can only do it on Firefox. On Chrome, the trigger click event doesn't work, and on IE8, it works but cannot upload the selected file. What should I do? This is my code in handleAjaxResponseSuccess function:

$('#inputFile').attr('accept', '.jpg, .png');
$('#inputFile').click();
//in Firefox and IE8, it shows a file dialog that allows choosing file to upload. But in Chrome, the file dialog does not appear, and in IE8, the selected file cannot be uploaded

My HTML code

<button type="button" id="uploadBtn" onclick="getAcceptedExtension()"title="Upload" class=""></button>
<input type="file" name="" id="inputFile" multiple="multiple"style="display: none;" >

I am using jQuery and Ajax on a button, to update the value of attribute "accept" on an input file upload element, then trigger click on it. But I can only do it on Firefox. On Chrome, the trigger click event doesn't work, and on IE8, it works but cannot upload the selected file. What should I do? This is my code in handleAjaxResponseSuccess function:

$('#inputFile').attr('accept', '.jpg, .png');
$('#inputFile').click();
//in Firefox and IE8, it shows a file dialog that allows choosing file to upload. But in Chrome, the file dialog does not appear, and in IE8, the selected file cannot be uploaded

My HTML code

<button type="button" id="uploadBtn" onclick="getAcceptedExtension()"title="Upload" class=""></button>
<input type="file" name="" id="inputFile" multiple="multiple"style="display: none;" >
Share Improve this question edited Nov 21, 2017 at 10:30 omglolgg asked Nov 21, 2017 at 10:25 omglolggomglolgg 1351 silver badge10 bronze badges 7
  • On Chrome, the trigger click event doesn't work this is by design. You cannot fake events on file inputs for security reasons in some browsers. accept only works in IE10 and above, and not in Edge at all. See MDN patibility table: developer.mozilla/en-US/docs/Web/HTML/Element/… – Rory McCrossan Commented Nov 21, 2017 at 10:27
  • I can see this. Is there any solution for this case? – omglolgg Commented Nov 21, 2017 at 10:31
  • Not reliably, no. Even if there is, it's likely to be broken by future browser updates. You need to create your UI so that the user must initiate the fie open dialog – Rory McCrossan Commented Nov 21, 2017 at 10:32
  • So you mean that the user have to directly click on the input file, right? – omglolgg Commented Nov 21, 2017 at 10:33
  • Yes, that's right – Rory McCrossan Commented Nov 21, 2017 at 10:33
 |  Show 2 more ments

2 Answers 2

Reset to default 2

For the security reasons some browsers don't allow to trigger file input directly. The action to open the dialog box MUST be called by a user interaction (a click for instance) AND the input file MUST be visible (display != none) and focused.

You can show to open dialog and after hide like this:

getAcceptedExtension = function() {
  $('#inputFile').attr('accept', '.jpg, .png');
  $('#inputFile').show();
  $('#inputFile').focus();
  $('#inputFile').click();
  $('#inputFile').hide();
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="uploadBtn" onclick="getAcceptedExtension()"title="Upload" class="">CLICK ME</button>
<input type="file" name="" id="inputFile" multiple="multiple"style="display: none;" >

one basic and easy way would be this:

$('#b1').on('click',function(){
    alert("button #1 is clicked");
    $('#b2').click();
});
$('#b2').on('click',function(){
    alert("button #2 is clicked");
});

Jsfiddle: https://jsfiddle/vf65pzhj/1/

发布评论

评论列表(0)

  1. 暂无评论