简体   繁体   English

当javascript触发对话框打开时,如何使IE8 / 9提交输入类型=“文件”

[英]How to make IE8/9 submit input type=“file” when javascript triggered dialog open

If I have an input type="file" somewhere, and on click of some link it also triggers a click on the file input. 如果我在某处有一个input type="file" ,并且点击某个链接它也会触发对文件输入的单击。 The user picks a file. 用户选择一个文件。

In IE8/9 if the user clicks on a button type="submit" the first click clears all data in the file input, then the second submits a blank form. 在IE8 / 9中,如果用户点击button type="submit"则第一次单击会清除文件输入中的所有数据,然后第二次提交一个空白表单。

How do I allow submission of the input type="file" post a javascript click trigger. 如何在javascript点击触发器后发布input type="file"

Some notes: 一些说明:

  • The click was triggered as a consequence of another click event. 由于另一次点击事件,点击被触发。 Within the event. 在活动中。
  • The input type="file" is not set to display: none. 输入类型=“文件”未设置为display:none。
  • No attempt to manipulate the user input was done 没有尝试操纵用户输入

Ideas? 想法?

I've been struggling with the same problem, and I'm yet to come across a clean solution. 我一直在努力解决同样的问题,我还没有遇到一个干净的解决方案。

I have however, discovered a work-around. 然而,我发现了一种解决方法。 Please note the the solutions below completely disregard best practices (in my humble opinion) and if anyone has a better, more standards-compliant way of doing this, please post it here. 请注意下面的解决方案完全忽略了最佳实践(以我的拙见),如果有人有更好的,更符合标准的方法,请在此处发布。

Fixing the button click event 修复按钮单击事件

After a fair amount of research, it seems a lot of people are using a trick whereby they absolutely position the <input type="file" /> immediately over their button, and set the opacity to zero. 经过大量研究后,似乎很多人都在使用一种技巧,即将<input type="file" />立即放在按钮上,并将不透明度设置为零。 This means that, to the user, it looks like they are clicking on a button, but the browser sees it as a click on the <input type="file" /> (and thus avoids the mentioned issue in IE). 这意味着,对于用户来说,看起来他们正在点击按钮,但是浏览器将其视为点击<input type="file" /> (从而避免了IE中提到的问题)。 I was surprised to see that Quirksmode actually recommended this approach. 我惊讶地发现Quirksmode实际上推荐了这种方法。

Fixing the button styles 修复按钮样式

Now, this does introduce further cosmetic problems; 现在,这确实引入了进一步的美容问题; you may find that your buttons no longer trigger their :hover and :active CSS pseudo classes (unless you placed the <input type="file" /> inside your button), and that your mouse cursor will probably be the default mouse cursor, despite any CSS you've applied to your <input type="file" /> or your button. 您可能会发现您的按钮不再触发它们:hover:active CSS伪类(除非您在按钮内放置<input type="file" /> ),并且您的鼠标光标可能是默认的鼠标光标,尽管有任何CSS你已经应用于<input type="file" />或你的按钮。

The classes are easy fix with a JavaScript work-around (I'll leave that to you), but the cursor is still an issue. 通过JavaScript解决这些类很容易解决(我会留给你),但光标仍然是一个问题。 Strangely, you can style the cursor of the right hand path side of the <input type="file" \\> , but this will not affect it's Browse button. 奇怪的是,您可以为<input type="file" \\>的右侧path侧的cursor设置样式,但这不会影响它的“ Browse按钮。 Unfortunately this (now transparent) button will probably be over some part of your styled button, resulting in a default cursor on hover. 不幸的是,这个(现在是透明的)按钮可能会超出样式按钮的某些部分,导致悬停时出现默认光标。

To solve this, I stumbled across this work-around (JSFiddle here ). 为了解决这个问题,我偶然发现了这种解决方法这里是 JSFiddle)。 Basically, someone has bound an event to the mousemove event of their button, and positioned the right-hand side of <input type="file" /> under the mouse cursor every time they hover over the button. 基本上,有人将事件绑定到其按钮的mousemove事件,并且每次将鼠标悬停在按钮上时,将鼠标光标放在<input type="file" />的右侧。 This may also need to be enhanced a bit in order to stop the <input type="file" /> from being clicked on from outside of the button's co-ordinates if the button does not have a fixed size and overflow: hidden set. 如果按钮没有固定大小和overflow: hidden set,则可能还需要增强一点,以便阻止<input type="file" />从按钮的坐标外部点击。

Conclusion 结论

Overall, I've found that styling <input type="file" /> is just not worth it; 总的来说,我发现造型<input type="file" />是不值得的; there are just too many hacks, and you're probably going to have to use JavaScript at some point if you really want it to look good (not great if you're trying to support people who have JavaScript disabled). 如果你真的希望它看起来不错,那么你可能会在某些时候使用JavaScript(如果你试图支持禁用JavaScript的人,那就不好了)。 I also have no idea how this affect mobile either. 我也不知道这对移动设备有何影响。

If you're targeting newer browsers that support the Html5 File API , implement an AJAX drag / drop upload solution ( here is a nice one), and get rid of the <input type="file" /> altogether. 如果您的目标是支持Html5文件API的新浏览器,请实施一个AJAX拖放上传解决方案( 是一个很好的解决方案),并完全摆脱<input type="file" /> For those that don't support it (older browser and those with JavaScript disabled), just leave the unstyled <input type="file" /> there as a fallback. 对于那些不支持它的人(较旧的浏览器和那些禁用了JavaScript的浏览器),只需将没有样式的<input type="file" />作为后备保留。

I found that using the jquery fileupload extension is just better. 我发现使用jquery fileupload扩展更好。 It handles the intricacies much much better than I could had I been doing it manually. 它比我手动操作时更能处理错综复杂的错误。 It basically does what jquery does for x-browser dom manipulation & ajax but for input type="file". 它基本上做了jquery为x-browser dom操作和ajax做的事情,但输入type =“file”。

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

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