简体   繁体   English

文件输入javascript单击不是从真正的鼠标单击镶边生成的

[英]file input javascript click generated not from a real mouse click chrome

i'm having trouble in chrome opening the popup for the file upload of a file input type. 我在Chrome打开文件输入类型的文件上传弹出窗口时遇到麻烦。

As you can see here: http://jsfiddle.net/cavax/K99gg/3/ , clicking on an elements can trigger a click on the file input, but for example hovering a element wont trigger a click on the input. 如您在此处看到的: http : //jsfiddle.net/cavax/K99gg/3/ ,单击元素可以触发对文件输入的单击,但是例如,悬停元素不会触发对输入的单击。

$('#test').on('click', function(){
   $('#upload').trigger('click');
});
$('#test').on('mouseenter', function(){
   $('#upload').trigger('click');
});

In the real life i'm having this trouble because in a web app i'm loading throw ajax a content witch has inside an input file. 在现实生活中,我遇到了麻烦,因为在Web应用程序中,我正在加载throw Ajax,一个内容女巫在输入文件中包含内容。 At the end of the load the popup file must open, but there is no way to get this works on Chrome (workign on FF). 在加载结束时,必须打开弹出文件,但是无法在Chrome上使它正常工作(在FF上有效)。

The problem i guess is that the action is not generated by a mouse click (hover, timeout etc) so chrome wont trigger the fileupload. 我猜的问题是该动作不是由鼠标单击(悬停,超时等)生成的,因此chrome不会触发fileupload。

I've also tryed this: http://jsfiddle.net/cavax/K99gg/7/ , so click on the element, wait then trigger the click but nothing, because there is the "settimeout" in the middle of the click and the trigger :( 我也尝试过这种方法: http : //jsfiddle.net/cavax/K99gg/7/ ,所以单击元素,等待然后触发单击,但是什么也没有,因为单击中间存在“ settimeout”,触发 :(

$('#test').on('click', function(){
  setTimeout(function(){
    $('#upload').trigger('click');
  }, 3000);
});

if you remove the delay: http://jsfiddle.net/cavax/K99gg/8/ it works Any idea to get this work? 如果您删除了延迟: http : //jsfiddle.net/cavax/K99gg/8/可以用知道要完成这项工作吗?

If I remember correctly, mouseenter and mouseleave are specific to IE, not standard events. 如果我没记错的话,mouseenter和mouseleave特定于IE,而不是标准事件。 Maybe they were extended, but don't think they became a standard. 也许它们得到了扩展,但不要认为它们已成为标准。 So the event itself may generate you some problems. 因此,事件本身可能会给您带来一些问题。

To resolve this you can use a lib (like jQuery for example), that treats the browser differences (or you can check the code there and take what you need). 要解决此问题,您可以使用一个库(例如jQuery),该库可以处理浏览器的差异(或者您可以在此处检查代码并获取所需的内容)。

Second way... try mouseover... it worked better (again... didn't work with them for a while so things may have happened, but this is how I remember them to be). 第二种方式...尝试将鼠标悬停...效果更好(再次...一段时间没有与他们合作,所以事情可能发生了,但这就是我记得的样子)。

There is no way to trigger click event of input file type , because of a security reason. 出于安全原因,无法触发input file type click事件。

You may try a hack of this by setting your button/div position to absolute and top to -100px It means positioning it outside the viewport by setting above style make it works. 您可以通过将button/div位置设置为absolutetop-100px-100px这意味着可以通过设置上述样式将其-100px在视口之外,以使其起作用。

But for mouseenter and mouseover i don't think it's going to work! 但是对于mouseentermouseover我认为它不会起作用!

Edit: 编辑:

Moved input outside the viewport and target click event 将输入移到视口和目标单击事件之外

Example on click 点击示例

Side note: Right now click occurs 2 times as you have written 旁注:现在, click发生的时间是您编写的2次

$('#upload').trigger('click').click();

You just need 您只需要

$('#upload').trigger('click');  //  $('#upload').click() 

unless you want it to fire more than single time. 除非您希望它触发一次以上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM