简体   繁体   English

jQuery.trigger('click') 在我的代码中不起作用

[英]jQuery.trigger('click') does not work in my code

I want the file explorer to pop up for user selecting files when user click a button which is a <input id="J_upload_btn" type="button" /> .当用户单击<input id="J_upload_btn" type="button" />按钮时,我希望弹出文件资源管理器供用户选择文件。

However the source above only works well on Firefox 4. On Chrome 11/12 and Firefox 3, the selecting-file-box pops up only after you click the button several times.然而,上面的源代码只在 Firefox 4 上运行良好。在 Chrome 11/12 和 Firefox 3 上,选择文件框只有在你点击按钮几次后才会弹出。 My jQuery version is 1.5.2.我的 jQuery 版本是 1.5.2。

$('#J_upload_btn').click(function() {
    var _id = new Date().getTime(),
        _$form = $('#J_pic_form'),
        _$input = $('<input id="' + _id + '"' +
                  ' type="file" name="file" class="hide_input">');
    _$form.append(_$input);
    _$input.trigger('click');
    }
});
  • Unmatched closing curly brace .无与伦比的右花括号 Just removing it should work.只需将其删除即可。
  • You cannot reliably access DOM elements before the DOM tree is created .在创建 DOM 树之前,您无法可靠地访问 DOM 元素 If this code is not wrapped in a function and called after that is the case, simply wrapping it in $(function(){ /* code */ });如果此代码未包装在 function 中并在此之后调用,则只需将其包装在$(function(){ /* code */ }); is enough for jQuery to call it as soon as the DOM is ready. jQuery 在 DOM 准备好后立即调用它就足够了。
  • In some browsers (including Firefox 3), triggering click events on <input type="file"> elements is restricted for security reasons .在某些浏览器(包括 Firefox 3)中, 出于安全原因,限制<input type="file">元素上触发点击事件。

This code works for me in Chrome:这段代码在 Chrome 中适用于我:

$(function() {
    $('#J_upload_btn').click(function() {
        var _id = new Date().getTime(),
            _$form = $('#J_pic_form'),
            _$input = $('<input id="' + _id + '"' +
                        ' type="file" name="file" class="hide_input">');
        _$form.append(_$input);
        _$input.trigger('click');
    });
});

This kind of input is more restricted than others, by security issues .由于安全问题,这种输入比其他输入更受限制。 Sure there's a more current method, but the most common method to do this is using an invisible input (with opacity 0, not display:none), and placing a fake button over it, so when you click on the fake button, you're also clicking on the invisible input.当然有一个更新的方法,但最常见的方法是使用不可见的输入(不透明度为 0,不显示:无),并在其上放置一个假按钮,所以当你点击假按钮时,你'重新点击不可见的输入。

You may want to try not wrapping the input as a jayesh object.您可能想尝试不将输入包装为 jayesh object。 If you pass the HTML string to append, then the browser should add a new input field too.如果您将 HTML 字符串传递给 append,那么浏览器也应该添加一个新的输入字段。

var input = "<input id='"+id+"' />";
_$form.append(input);

Also, have you tried debugging the click in a console to make sure that it is getting fired or the subsequent code has the issue?此外,您是否尝试过在控制台中调试单击以确保它被触发或后续代码有问题?

If you are tiggering click on the input, a function is expected to run.如果您点击输入,function 预计会运行。 But as far as I can see, you have not written any function for the click event of _$input.但是据我所见,您还没有为_$input的点击事件写任何function。

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

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