开发者

Open html file dialog with jquery

I am trying to open input file dialog after clicki开发者_JAVA百科ng on link. I made a script with jquery. But I also want to avoid opening this dialog after clicking on input file:

            $('#link').click(function(event) {
                event.preventDefault();
                $('#id_default_image').click();
            });
            $('.file_input').click(function(event) {
                event.preventDefault();
            });

Now when I click on link or input file dialog does not show. Can I check if user clicks on link or on input and show dialog or not?


You can use global flag for this, and raise it when clicking the link.

Code will now be:

$('#link').click(function(event) {
    event.preventDefault();
    window["link_clicked"] = true;
    $('#id_default_image').click();
    window["link_clicked"] = false;     
});

And to check the flag:

$('.file_input').click(function(event) {
    if (window["link_clicked"]) {
        alert("you clicked the link");
    }
    //event.preventDefault();
});

Live test case: http://jsfiddle.net/trG5D/1/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜