How to trigger checkbox click event even if it's checked through Javascript code?

前端 未结 7 1393
灰色年华
灰色年华 2021-01-30 19:39

I have many checkboxes in my page and there is a select all checkbox which checks all the checkboxes. Somehow I want to emulate that click event of checkbox even if it\'s checke

7条回答
  •  忘掉有多难
    2021-01-30 19:56

    Trigger function from jQuery could be your answer.

    jQuery docs says: Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user

    Thus best one line solution should be:

    $('.selector_class').trigger('click');
    
    //or
    
    $('#foo').click();
    

提交回复
热议问题