简体   繁体   中英

How do i trigger input file manually?

<input type="text" />
<input type="file" />

$('input[type=text]').click(function() {
    $('input[type=file]').trigger('click');
});

I can get browse option (open dialog box) when I click the test box. But I cannot get browse option when I trigger text box's click using jquery trigger method.

$('input[type=file]').trigger('click');

How do i solve this?

What helped for me is to set the event-listener inside:

$(document).ready(function(){
    $('input[type=text]').click(function() {
        $('input[type=file]').trigger('click');
    });
}};

That used to do the trick for me. You might try it as well.

You can wrap your code inside DOM ready handler $(function() {...}); to make sure your DOM elements are loaded properly before executing your jQuery code.

$(function(
    $('input[type=text]').click(function() {
        $('input[type=file]').trigger('click');
    });
)};

My belief is your selector $('input[type=text]') is not selecting , give it some id and use it like

$("#text").click(function(){
     $("#file").trigger("click");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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