Jquery 触发点击在 mac、Ipad 和 Safari 浏览器上不起作用IPhone

发布于 2024-12-28 00:21:31 字数 200 浏览 2 评论 0原文

我试图在单击另一个按钮时触发输入类型=“文件”上的单击事件。

演示: http://jsfiddle.net/Y85g6/

它在所有浏览器中都能正常工作,除了 mac、Ipad 和 Safari 浏览器之外。 IPhone。

有什么技巧可以完成这个任务吗?

I am trying to trigger a click event on input type="file" on the click of an another button.

Demo: http://jsfiddle.net/Y85g6/

It's working fine in all browsers except on safari browsers in mac, Ipad & Iphone.

Is there any trick to accomplish this task?

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

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

发布评论

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

评论(11

小ぇ时光︴ 2025-01-04 00:21:31

您可以使用以下在所有浏览器中都能成功运行的代码,而不是 trigger("click")

var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, true); 
document.getElementById(elem_id).dispatchEvent(evt);

Instead of trigger("click") you can use the following code which works successfully in all browsers:

var evt = document.createEvent("HTMLEvents");
evt.initEvent("click", true, true); 
document.getElementById(elem_id).dispatchEvent(evt);
⊕婉儿 2025-01-04 00:21:31

找到了替代方案。

只需通过绝对定位将输入 type="file" 定位在自定义按钮上,然后使用 jQuery fadeTo('fast',0) 隐藏它。

现在,如果我们单击自定义按钮,则会出现文件浏览器窗口。

它适用于所有桌面浏览器,但不适用于 iPhoneiPad 因为它们不允许上传任何文件。

Found an alternative.

Just position the input type="file" over the custom button by absolute positioning it and use jQuery fadeTo('fast',0) to hide it.

Now if we click over the custom button file browser window appears.

Its working in all desktop browsers but not in iPhone & iPad as they don't allow to upload any file.

遥远的绿洲 2025-01-04 00:21:31

使元素可见,因为触发事件不适用于 mac safari 中的隐藏元素。

make the element visible, as trigger event doesnt work on hidden elements in mac safari.

情绪少女 2025-01-04 00:21:31

以下方法对我有用:

$(".upload_on_click").each(function(index) {
    var form = $(this).next("form");
    //form.hide(); /* THIS TECHNIQUE DIDN'T WORK IN SAFARI */
    form.css("position", "absolute");
    form.css("visibility", "hidden");
    $(this).css("cursor", "pointer");
    $(this).click(function() {
        $('input[type="file"]', form).trigger("click");
    });
    $('input[type="file"]', form).change(function() {
        $('input[type="submit"]', form).trigger("click");
    });
});

The following approach did the trick for me:

$(".upload_on_click").each(function(index) {
    var form = $(this).next("form");
    //form.hide(); /* THIS TECHNIQUE DIDN'T WORK IN SAFARI */
    form.css("position", "absolute");
    form.css("visibility", "hidden");
    $(this).css("cursor", "pointer");
    $(this).click(function() {
        $('input[type="file"]', form).trigger("click");
    });
    $('input[type="file"]', form).change(function() {
        $('input[type="submit"]', form).trigger("click");
    });
});
半衾梦 2025-01-04 00:21:31

出于安全原因,浏览器在 JavaScript 与文件输入交互时可能非常挑剔。 Safari 会阻止您触发它们上的任何点击事件。 Chrome 和 Firefox 的某些版本也有此限制。这是之前此处讨论

Browsers can be very finicky when it comes to JavaScript interactions with file inputs, for security reasons. Safari prevents you from firing any click events on them. Some versions of Chrome and Firefox also have this restriction. This has been previously discussed here.

长安忆 2025-01-04 00:21:31

最好使用CSS而不是JS来隐藏元素,因为它会直接将你的元素渲染为隐藏。

Its better to use CSS instead of JS to hide element because it will render your element as hidden directly.

等风也等你 2025-01-04 00:21:31

不确定是否有人再读这个问题,但我最近在 Safari 上遇到了类似的问题,所以我想我会分享。

首先,正如 ceejayoz 提到的,请参阅此处的讨论:在 JavaScript 中,我可以以编程方式为文件输入元素触发“单击”事件吗?

那么,如果您不想使用按钮定位,则解决方案是显示文件输入按钮(删除“display:none;”) 并使用“opacity:0;”隐藏它。您可能还想对其进行绝对定位,这样它就不会与其他元素交互。不管怎样,这样你仍然可以在所有浏览器中使用JS来激活文件输入。

Not sure if anybody is reading this question anymore, but I recently had a similar issue with Safari, so I thought I'd share.

First, as ceejayoz mentioned, see the discussion here: In JavaScript can I make a "click" event fire programmatically for a file input element?

The solution, then, if you do not want to use button positioning, is to display the file input button (remove the "display:none;") and instead hide it using "opacity:0;". You probably also want to absolute position it so it doesn't interact with other elements. Anyway, this way you can still use JS to activate the file input in all browsers.

情泪▽动烟 2025-01-04 00:21:31

Safari 不支持 .click()。萨图的建议应该有效。但您不需要 Javascript 来自定义输入文件按钮。

<label><input type="file" id="file" name="file" style="position:absolute; left:-9999px;" />Upload</label>

参考:http://noypiscripter.blogspot.com/2014 /04/simplest-cross-browser-custom-upload.html

.click() is not supported in Safari. Sattu's suggestion should work. But you don't need Javascript for customizing input file button.

<label><input type="file" id="file" name="file" style="position:absolute; left:-9999px;" />Upload</label>

Reference: http://noypiscripter.blogspot.com/2014/04/simplest-cross-browser-custom-upload.html

冷夜 2025-01-04 00:21:31

得到了这个 opacity : 0.01 最简单的答案

我在输入文件元素上

I got the simplest answer for this

opacity : 0.01 on your input file element

请爱~陌生人 2025-01-04 00:21:31

只需删除“display:none”并使用“visibility:hidden”即可跨浏览器工作。

simply remove "display:none" and use "visibility:hidden" and works cross browser.

烂柯人 2025-01-04 00:21:31

尝试在页脚中加载脚本。我遇到过几次类似的问题,这成功了。

Try loading your script in the footer. I had a similar problem a few times and that did the trick.

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