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

前端 未结 28 1404
囚心锁ツ
囚心锁ツ 2020-11-21 06:29

I\'d like to make a click event fire on an tag programmatically.

Just calling click() doesn\'t seem to do anything or at lea

28条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 07:03

    You can fire click() on any browser but some browsers need the element to be visible and focused. Here's a jQuery example:

    $('#input_element').show();
    $('#input_element').focus();
    $('#input_element').click();
    $('#input_element').hide();
    

    It works with the hide before the click() but I don't know if it works without calling the show method. Never tried this on Opera, I tested on IE/FF/Safari/Chrome and it works. I hope this will help.

提交回复
热议问题