调用点击事件输入在Safari中不起作用

我有一个输入,文件类型, display:none ,还有一个按钮。 单击按钮后,应触发输入的事件。 在IE浏览器,Chrome和Firefox中它可以工作,但在Safari中却不行!

 var elem=$(''); if($("#ajxAttachFiles").length==0){ elem.prependTo(".ChProgress"); } $("#ajxAttachFiles").click(); 

控制台中没有错误。 我尝试了这个,但没有。

 $(document).ready(function(){ $("#btn").on('click',function(){ $("#ajxAttachFiles")[0].click(); }); }); 
    

这个问题彻底解决了。 点是input[type=file]元素,不能是display:none 。 看下面的样本

 function click(el) { // Simulate click on the element. var evt = document.createEvent('Event'); evt.initEvent('click', true, true); el.dispatchEvent(evt); } document.querySelector('#selectFile').addEventListener('click', function(e) { var fileInput = document.querySelector('#inputFile'); //click(fileInput); // Simulate the click with a custom event. fileInput.click(); // Or, use the native click() of the file input. }, false); 
   

触发DOM元素而不是jQuery对象的click事件。 这应该适用于所有浏览器。

  $('#ajxAttachFiles')[0].click(); 

要么:

 document.getElementById('ajxAttachFiles').dispatchEvent( new Event('click') ); //for IE < 9 use microsoft's document.createEventObject 
 var elem=$(''); if($("#ajxAttachFiles").length==0){ elem.prependTo(".ChProgress"); } $("#ajxAttachFiles").on('click',function() { alert( 'click triggered' ); }); $("#ajxAttachFiles")[0].click(); //an alternative: var event = new Event('click'); document.getElementById('ajxAttachFiles').dispatchEvent(event); 
  
Will be added here