jQuery 触发 uploadify 点击事件在 Firefox FF 中不起作用

发布于 2024-10-17 00:14:28 字数 1019 浏览 1 评论 0原文

我想在下拉框中选择一个选项,并为此触发 jQuery 可用的 uploadify,从而允许您上传文件。

我的解决方案适用于 IE7,但不适用于 FF。

当您更改下拉列表时,它应该显示一个窗口来浏览要上传的文件。 FF 中什么也没有出现。在 IE 中一切正常。

JS 在 FF 中启用,如果我插入警报消息,它就会触发输入按钮的点击。

<select class="selectLogoTop" name="selectLogoTop">
   <option value="0">0</option>
   <option value="1">1</option>
</select>

<input class="fileupload1" type="file" />

<script type="text/javascript">
   $(document).ready(function() {
      $('.fileupload1').uploadify({
        'uploader'       : '../../../admin/uploadFileResources/uploadify.swf',
        'script'         : '../../../admin/uploadFileResources/upload.cfm',
        'cancelImg'      : '../../../admin/uploadFileResources/cancel.png',
        'folder'         : '../../../upload_BE/offers/htmlfiles/5953/images/',
        'multi'          : true
      });

      $('.selectLogoTop').change(function(){
         $('.fileupload1').trigger("click");
      });
   });
</script>

I want to select an option on a drop down box and for this to trigger the uploadify available to jQuery which lets you upload a file.

My solution works in IE7 but not FF.

When you change the drop down it should show a window to browse for a file to upload. In FF nothing appears. In IE everything works.

JS is enabled in FF, if I insert alert messages it gets to the point of triggering the click on the input button.

<select class="selectLogoTop" name="selectLogoTop">
   <option value="0">0</option>
   <option value="1">1</option>
</select>

<input class="fileupload1" type="file" />

<script type="text/javascript">
   $(document).ready(function() {
      $('.fileupload1').uploadify({
        'uploader'       : '../../../admin/uploadFileResources/uploadify.swf',
        'script'         : '../../../admin/uploadFileResources/upload.cfm',
        'cancelImg'      : '../../../admin/uploadFileResources/cancel.png',
        'folder'         : '../../../upload_BE/offers/htmlfiles/5953/images/',
        'multi'          : true
      });

      $('.selectLogoTop').change(function(){
         $('.fileupload1').trigger("click");
      });
   });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自唱情﹋歌 2024-10-24 00:14:28

根据这个问题,设计时不可能触发文件输入的点击(由于安全问题)。虽然它在 Chrome 中确实有效,但我在 Firefox(以及 Opera)上也没有成功使用此代码。

<select class="selectLogoTop" name="selectLogoTop">
   <option value="0">0</option>
   <option value="1">1</option>
</select>

<input class="fileupload1" type="file" />

<script type="text/javascript">
   $(document).ready(function() {
      $('.selectLogoTop').change(function(){
         $('.fileupload1').trigger("click");
      });
   });
</script>

为什么需要用链接触发文件输入?如果是由于可应用于输入的样式有限,这篇文章< /a> 在另一个问题上提到过。

此外,可能不相关,但 Uploadify 文档说,您需要为文件输入提供唯一的 ID。你的只有一个班级。

Uploadify 的每个元素
应用于必须有一个唯一的ID
属性。您可以参考元素
通过类,但每个元素必须有
唯一的 ID。

According to this question it's not possible to trigger a click on a file input by design (due to security issues). While it does work in Chrome, I had no luck with this code on Firefox (and Opera as well) either.

<select class="selectLogoTop" name="selectLogoTop">
   <option value="0">0</option>
   <option value="1">1</option>
</select>

<input class="fileupload1" type="file" />

<script type="text/javascript">
   $(document).ready(function() {
      $('.selectLogoTop').change(function(){
         $('.fileupload1').trigger("click");
      });
   });
</script>

Why do you need to trigger the file input with a link? If it's due to the limited styling that can be applied to the input, this article was mentioned on the other question.

Besides, possibly not related, but the Uploadify documentation says, you need to give the file input a unique ID. Yours only has a class.

Every element that Uploadify is
applied to MUST have a unique ID
attribute. You can reference elements
via class, but each element must have
a unique ID.

伴随着你 2024-10-24 00:14:28

这在 Firefox 中是不可能的。然而,看看拉马斯在回答同样的问题时给出的巧妙的解决方法:

在 JavaScript 中,我可以通过编程方式为文件输入元素触发“click”事件吗?

抱歉,请忽略它没有执行您想要的操作!

在这种情况下,我会这样做:

将您的代码行替换

$('.fileupload1').trigger("click"); 

为:

$('.fileupload1').fadeOut(300).delay(100).fadeIn(300, function(){$(this).focus().trigger("click")});

这将导致 INPUT 元素闪烁(您可以将其修改为闪烁几次),并且它将获得焦点,从而在Firefox 通过改变它的颜色。这样(假设它靠近选择框),用户将看到他需要按下输入元素的“浏览”按钮。我知道这并不完美,但它是一个可行的方法。

请参阅以下位置的示例:

http://www.jsfiddle.net/elusien/6XrSS/2/


问候
尼尔

This is not possible in Firefox. However, take a look at the neat workaround given by Ramas in response to the same question here:

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

Sorry, IGNORE THAT it doesn't do what you want!

HERE IS WHAT I WOULD DO in this case:

replace your line of code that says:

$('.fileupload1').trigger("click"); 

by:

$('.fileupload1').fadeOut(300).delay(100).fadeIn(300, function(){$(this).focus().trigger("click")});

This will cause the INPUT element to flash (you could modify it to flash a couple of times) and it will receive focus, which highlights it in Firefox by changing its colour. In that way (provided it is close to the SELECT box), the user will see that he needs to press the "BROWSE" button of the INPUT element. I know this isn't perfect, but it is a workable fudge.

See an example at:

http://www.jsfiddle.net/elusien/6XrSS/2/

Regards
Neil

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文