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

前端 未结 7 1291
灰色年华
灰色年华 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:54

    no gQuery

    document.getElementById('your_box').onclick();

    I used certain class on my checkboxes.

    var x = document.getElementsByClassName("box_class");
    var i;
    for (i = 0; i < x.length; i++) {
        if(x[i].checked) x[i].checked = false;
        else x[i].checked = true;
        x[i].onclick();
       }
    

提交回复
热议问题